### constant

MOD = 2011

### subroutines

def get_base(strs, topl, btml)
  base = nil
  while base.nil?
    for y in (topl..btml)
      if strs[y][$pos] != '.'
        base = y
        break
      end
    end
    $pos += 1 if base.nil?
  end
  base
end

def expr(strs, topl, btml, base = nil)
  #p ["expr", topl, btml, base, $pos]

  base = get_base(strs, topl, btml) if base.nil?
  
  v = term(strs, topl, btml, base)
  op = strs[base][$pos + 1]
  while op == '+' || op == '-'
    $pos += 3
    v0 = term(strs, topl, btml, base)
    v = (op == '+') ? (v + v0) % MOD : (v - v0) % MOD
    op = strs[base][$pos + 1]
  end

  #p ["  expr", v, base]
  v
end

def term(strs, topl, btml, base)
  #p ["term", topl, btml, base, $pos]

  v = factor(strs, topl, btml, base)
  while strs[base][$pos + 1] == '*'
    $pos += 3
    v0 = factor(strs, topl, btml, base)
    v = (v * v0) % MOD
  end

  #p ["  term", v, $pos]
  v
end

def factor(strs, topl, btml, base)
  #p ["factor", topl, btml, base, $pos]

  v = nil
  if strs[base][$pos, 2] == '--'
    v = fraction(strs, topl, btml, base)
  elsif strs[base][$pos, 2] == '-.'
    $pos += 2
    v = MOD - factor(strs, topl, btml, base)
  else
    v = powexpr(strs, topl, btml, base)
  end

  #p ["  factor", v, $pos]
  v
end

def powexpr(strs, topl, btml, base)
  #p ["powexpr", topl, btml, base, $pos]

  v = primary(strs, topl, btml, base)

  if base > 0 && strs[base - 1][$pos] =~ /[0-9]/
    n = digit(strs, topl, btml, base - 1)

    pm = 1
    while n > 0
      pm = (pm * v) % MOD if (n & 1) == 1
      v = (v * v) % MOD
      n >>= 1
    end

    v = pm
  end

  #p ["  powexpr", v, $pos]
  v
end

def primary(strs, topl, btml, base)
  #p ["primary", topl, btml, base, $pos]

  v = nil
  if strs[base][$pos] == '('
    $pos += 2
    v = expr(strs, topl, btml, base)
    $pos += 2
  else
    v = digit(strs, topl, btml, base)
  end

  #p ["  primary", v, $pos]
  v
end

def fraction(strs, topl, btml, base)
  #p ["fraction", topl, btml, base, $pos]

  pos0 = $pos
  v0 = expr(strs, topl, base - 1)
  $pos = pos0
  v1 = expr(strs, base + 1, btml)
  $pos = pos0
  v = (v0 * $invs[v1]) % MOD

  while strs[base][$pos] == '-'
    $pos += 1
  end

  #p ["  fraction", v, $pos]
  v
end

def digit(strs, topl, btml, base)
  #p ["digit", topl, btml, base, $pos]

  v = strs[base][$pos].to_i
  $pos += 1

  #p ["  digit", v, $pos]
  v
end

### main

$invs = Array.new(MOD){0}
for y in (1...MOD)
  for z in (1...MOD)
    if (y * z) % MOD == 1
      $invs[y] = z
      break
    end
  end
end
#p $invs; exit

loop do
  n = gets.to_i
  break if n == 0

  strs = Array.new(n){gets.strip}
  #p strs

  $pos = 0
  p expr(strs, 0, n - 1)
end