ASCII Expression

No attempts yetTime limit1sMemory limit128 MB

Problem

Mathematical expressions in old papers and old technical articles were printed with a typewriter across several lines, which requires a fixed-width (monospace) font to place characters (digits, symbols, and spaces). Consider the following mathematical expression.

$$\left( 1 - \frac{4}{3^2}\right)^2 \times -5 + 6$$

It is printed in the following four lines:

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

Here - 5 means a unary minus followed by 5. We call such a multi-line expression an ASCII expression.

To help those who want to evaluate ASCII expressions (for example, obtained by OCR from old papers), your task is to write a program that recognizes the structure of an ASCII expression and computes its value.

For simplicity, you may assume that ASCII expressions are built only by the following rules. The syntax is shown in Table H.1.

Table H.1: Rules for building ASCII expressions (similar to Backus-Naur Form). Each box denotes a cell, a rectangular region of characters that corresponds to one terminal or nonterminal symbol. Every syntactically required space character is shown explicitly here as a period ..

  (I)     expr ::= term | expr.+.term | expr.-.term
 (II)     term ::= factor | term.*.factor
(III)   factor ::= powexpr | fraction | -.factor
                                    digit
 (IV)  powexpr ::= primary | primary
  (V)  primary ::= digit | (.expr.)
                      expr
 (VI) fraction ::= ---------
                      expr
(VII)    digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

Figure H.1: Top line, base line, and bottom line. Example cells — expr $1-\frac{4}{3^2}$, fraction $\frac{4}{3^2}$, powexpr $3^2$, digit $3$.

  1. The terminal symbols are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, *, (, ), and the space character.
  2. The 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 corresponding to one terminal or nonterminal symbol (Figure H.1). A cell has no redundant rows or columns that consist only of spaces. A cell for a terminal symbol is a single character. A cell for a nonterminal symbol contains the cell(s) of 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 the child cells on the right-hand side of rules I, II, III, and V are aligned. Their vertical position defines the base-line position of the left-hand-side cell.
  5. A powexpr consists of a primary and an optional digit. The digit is placed one line above the base line of the primary cell, and the two are horizontally adjacent. The base line of a powexpr is that of its primary.
  6. A fraction is indicated by three or more consecutive hyphens called a vinculum. Its dividend (numerator) expr is placed just above the vinculum, and its divisor (denominator) expr just below it. The number of hyphens in the vinculum, $w_h$, equals $w_h = 2 + \max(w_1, w_2)$, where $w_1$ and $w_2$ are the widths of the numerator cell and the denominator cell. Both cells are centered, with $\lceil (w_h - w_k)/2 \rceil$ spaces on the left and $\lfloor (w_h - w_k)/2 \rfloor$ spaces on the right ($k = 1, 2$). The base line of a fraction is at the vinculum.
  7. A digit consists of one character.

For example, the negative fraction $-\frac{3}{4}$ is written in three lines:

   3
- ---
   4

Here the leftmost hyphen is a unary minus operator. Exactly one space is required between the unary minus and the vinculum of the fraction.

The fraction $\frac{3+4 \times -2}{-1-2^2}$ is written in four lines:

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

Here the widths of the numerator and denominator cells are 11 and 8, so the vinculum has $2 + \max(11, 8) = 13$ hyphens. The denominator is centered with $\lceil (13 - 8)/2 \rceil = 3$ spaces on the left and $\lfloor (13 - 8)/2 \rfloor = 2$ on the right.

The powexpr $\left(4^2\right)^3$ is written in two lines:

   2  3
( 4  )

Here the cell for 2 is one line above the base line of the cell for 4, and the cell for 3 is one line above the base line of the cell for the primary $\left(4^2\right)$.

Input

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

n
str1
str2
.
.
.
strn

$n$ is a positive integer giving the number of the following lines, all of the same length, that represent one ASCII-expression cell. $str_k$ is the $k$-th line of the cell, where every space character has been replaced by a period.

You may assume that $n \le 20$ and that each line has length at most 80.

Output

For each dataset, output on one line a single non-negative integer less than 2011. This integer is the value of the ASCII expression under arithmetic modulo 2011. The output must contain no other characters.

There is no fraction whose divisor is zero or a multiple of 2011.

The powexpr $x^0$ is defined to be 1, and $x^y$ (for a positive integer $y$) is defined as the product $x \times x \times \cdots \times x$ of $y$ copies of $x$.

A fraction $\frac{x}{y}$ is computed as $x$ times the inverse of $y$ modulo 2011, i.e. $x \times \mathrm{inv}(y)$. Since 2011 is prime, the inverse of $y$ ($1 \le y < 2011$) is the unique integer $z$ ($1 \le z < 2011$) with $z \times y \equiv 1 \pmod{2011}$.