p00901 ASCII Expression
Mathematical expressions appearing in old papers and old technical articles are printed with typewriter in several lines, where a fixed-width or monospaced font is required to print characters (digits, symbols and spaces). Let us consider the following mathematical expression.

<image>

It is printed in the following four lines:


4   2
( 1 - ---- )  * - 5 + 6
2
3


where "- 5" indicates unary minus followed by 5. We call such an expression of lines "ASCII expression".

For helping those who want to evaluate ASCII expressions obtained through optical character recognition (OCR) from old papers, your job is to write a program that recognizes the structure of ASCII expressions and computes their values.

For the sake of simplicity, you may assume that ASCII expressions are constructed by the following rules. Its syntax is shown in Table H.1.

(1) | Terminal symbols are '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '-', '*', '(', ')', and ' '.
---|---
(2) | Nonterminal symbols are expr, term, factor, powexpr, primary, fraction and digit. The start symbol is expr.
(3) | A "cell" is a rectangular region of characters that corresponds to a terminal or nonterminal symbol (Figure H.1). In the cell, there are no redundant rows and columns that consist only of space characters. A cell corresponding to a terminal symbol consists of a single character. A cell corresponding to a nonterminal symbol contains cell(s) corresponding to its descendant(s) but never partially overlaps others.
(4) | Each cell has a base-line, a top-line, and a bottom-line. The base-lines of child cells of the right-hand side of rules I, II, III, and V should be aligned. Their vertical position defines the base-line position of their left-hand side cell.
Table H.1: Rules for constructing ASCII expressions (similar to Backus-Naur Form) The box indicates the cell of the terminal or nonterminal symbol that corresponds to a rectan- gular region of characters. Note that each syntactically-needed space character is explicitly indicated by the period character denoted by, here.
<image>
(5) | powexpr consists of a primary and an optional digit. The digit is placed one line above the base-line of the primary cell. They are horizontally adjacent to each other. The base-line of a powexpr is that of the primary.
(6) | fraction is indicated by three or more consecutive hyphens called "vinculum". Its dividend expr is placed just above the vinculum, and its divisor expr is placed just beneath it. The number of the hyphens of the vinculum, denoted by wh, is equal to 2 + max(w1, w2), where w1 and w2 indicate the width of the cell of the dividend and that of the divisor, respectively. These cells are centered, where there are ⌈(wh−wk)/2⌉ space characters to the left and ⌊(wh−wk)/2⌋ space characters to the right, (k = 1, 2). The base-line of a fraction is at the position of the vinculum. | (7) | digit consists of one character.



For example, the negative fraction<image> is represented in three lines:


3
- ---
4

where the left-most hyphen means a unary minus operator. One space character is required between the unary minus and the vinculum of the fraction.

The fraction <image> is represented in four lines:


3 + 4 * - 2
-------------
2
- 1 - 2


where the widths of the cells of the dividend and divisor are 11 and 8 respectively. Hence the number of hyphens of the vinculum is 2 + max(11, 8) = 13. The divisor is centered by ⌈(13−8)/2⌉ = 3 space characters (hyphens) to the left and ⌊(13−8)/2⌋ = 2 to the right.

The powexpr (42)3 is represented in two lines:


2  3
( 4  )


where the cell for 2 is placed one line above the base-line of the cell for 4, and the cell for 3 is placed one line above the base-line of the cell for a primary (42).



Input

The input consists of multiple datasets, followed by a line containing a zero. Each dataset has the following format.


n
str1
str2
.
.
.
strn


n is a positive integer, which indicates the number of the following lines with the same length that represent the cell of an ASCII expression. strk is the k-th line of the cell where each space character is replaced with a period.

You may assume that n ≤ 20 and that the length of the lines is no more than 80.

Output

For each dataset, one line containing a non-negative integer less than 2011 should be output. The integer indicates the value of the ASCII expression in modular arithmetic under modulo 2011. The output should not contain any other characters.

There is no fraction with the divisor that is equal to zero or a multiple of 2011.

Note that powexpr x0 is defined as 1, and xy (y is a positive integer) is defined as the product x×x×...×x where the number of x's is equal to y.

A fraction<image>is computed as the multiplication of x and the inverse of y, i.e., x× inv(y), under y modulo 2011. The inverse of y (1 ≤ y < 2011) is uniquely defined as the integer z (1 ≤ z < 2011) that satisfies z × y ≡ 1 (mod 2011), since 2011 is a prime number.

Example

Input

4
........4...2..........
(.1.-.----.)..*.-.5.+.6
........2..............
.......3...............
3
...3.
-.---
...4.
4
.3.+.4.*.-.2.
-------------
..........2..
...-.1.-.2...
2
...2..3
(.4..).
1
2.+.3.*.5.-.7.+.9
1
(.2.+.3.).*.(.5.-.7.).+.9
3
.2....3.
4..+.---
......5.
3
.2......-.-.3.
4..-.-.-------
..........5...
9
............1............
-------------------------
..............1..........
.1.+.-------------------.
................1........
......1.+.-------------..
..................1......
...........1.+.-------...
................1.+.2....
15
.................2......
................---.....
.......2.........5....3.
.(.---------.+.-----.)..
.....7...........3......
....---.+.1.............
.....4..................
------------------------
.......2................
......---...............
.......5.......2....2...
...(.-----.+.-----.)....
.......3.......3........
..............---.......
...............4........
2
.0....2....
3..+.4..*.5
20
............2............................2......................................
...........3............................3.......................................
..........----.........................----.....................................
............4............................4......................................
.....2.+.------.+.1...............2.+.------.+.1................................
............2............................2......................................
...........2............................2........................2..............
..........----.........................----.....................3...............
............2............................2.....................----.............
...........3............................3........................4..............
(.(.----------------.+.2.+.3.).*.----------------.+.2.).*.2.+.------.+.1.+.2.*.5
............2............................2.......................2..............
...........5............................5.......................2...............
..........----.........................----....................----.............
............6............................6.......................2..............
.........------.......................------....................3...............
............3............................3......................................
..........----.........................----.....................................
............2............................2......................................
...........7............................7.......................................
0


Output

501
502
1
74
19
2010
821
821
1646
148
81
1933
```python
MOD = 2011
while 1:
    n = int(input())
    if n == 0:
        break
    S = [input() for i in range(n)]
    w = len(S[0])

    def parse(bcur, bright, top, bottom):
        #print("parse", bcur, bright, top, bottom)
        base = -1
        for i in range(bcur, bright):
            for j in range(top, bottom+1):
                if S[j][i] != '.':
                    base = j
                    break
            if base != -1:
                break
        cur = bcur
        def read():
            nonlocal cur
            c = None
            while 0 <= base < n and cur < bright:
                #print("cur", base, cur, bright)
                #print(S[base][cur:])
                c = S[base][cur]
                if c != '.':
                    break
                cur += 1
            return c
        def fraction():
            nonlocal cur
            #print("fraction", cur, top, bottom)
            left = cur
            while cur < w and S[base][cur] == '-':
                cur += 1
            right = cur+1 if cur < w else cur
            dividend = parse(left, right, top, base-1)
            divisor = parse(left, right, base+1, bottom)
            #print("deb", S[base][cur:], dividend, divisor)
            #print("<fraction", dividend, divisor, (dividend * pow(divisor, MOD-2, MOD)) % MOD)
            return (dividend * pow(divisor, MOD-2, MOD)) % MOD
        def primary():
            nonlocal cur
            #print("primary", cur, top, bottom)
            c = read()
            if c == '(':
                cur += 1 # '('
                v = expr()
                cur += 1 # ')'
                #print("<primary", v)
                return v
            else:
                cur += 1 # digit
                #print("<primary", c)
                return int(c)
        def powexpr():
            nonlocal cur
            #print("powexpr", cur, top, bottom)
            v = primary()
            #print("<powexpr", cur, base, v)
            if 0 < base and cur < bright and S[base-1][cur] in "0123456789":
                #print("abc", v, int(S[base-1][cur]))
                return pow(v, int(S[base-1][cur]), MOD)
            return v
        def factor():
            nonlocal cur
            #print("factor", cur, top, bottom)
            c = read()
            if c == '-':
                if S[base][cur+1] == '.':
                    cur += 1 # '-'
                    return -factor()
                else:
                    return fraction()
            return powexpr()

        def term():
            nonlocal cur
            #print("term", cur, top, bottom)
            result = 1
            while 1:
                v = factor()
                result *= v
                result %= MOD
                c = read()
                if c != '*':
                    break
                cur += 1
            return result

        def expr():
            nonlocal cur
            #print("expr", cur, top, bottom)
            op = '+'
            result = 0
            while 1:
                v = term()
                #print("<expr", v)
                c = read()
                result += v if op == '+' else MOD-v
                result %= MOD
                if not c or c not in '+-':
                    #print("break", result, v, c, op)
                    break
                cur += 1
                op = c
            #print("<result", result)
            return result
        v = expr()
        #print("<parse", v)
        return v
    print(parse(0, w, 0, n-1))
```