Under construction

Still being built. Things may change

Evaluation

Time limit2sMemory limit512 MB

Summary
Evaluate an expression drawn as 2D ASCII art modulo 10^9+7, where the art encodes numbers, parentheses, fourth roots, operators, and fractions.
Level

Hard8 of 10

Topics
Implementation, Recursion, Number theory
Solved
No attempts yet

Problem

Two pictures are defined first.

  • S(r,c)S(r,c) is a picture of width cc and height rr made only of spaces.
  • VhV_h is a picture of width 1 and height hh made only of | characters.

An expression EE is one of the following five forms.

  1. Number. EE has the form below, and eval(E)=n mod p\mathrm{eval}(E) = n \bmod p. It is given that n≥0n \ge 0. Here h1,h2≥0h_1, h_2 \ge 0, and EE has width LL and height h1+h2+1h_1+h_2+1.
S(h1,L)S(h_1,L)
nn (length LL)
S(h2,L)S(h_2,L)
  1. Parentheses. eval(E)=eval(E′)\mathrm{eval}(E) = \mathrm{eval}(E').

If the height of E′E' is 1, EE has the form below, with width L+2L+2 and height 1.

(E' (width LL))

If the height of E′E' is H≥2H \ge 2, EE has the form below, with width L+2L+2 and height HH.

| / | E' (width LL) | \ | | VH−2V_{H-2} | | VH−2V_{H-2} | | \ | | / |

  1. Root. EE has the form below, and eval(E)=min⁡{0≤x<p:x4=eval(E′)2 mod p}\mathrm{eval}(E) = \min\{0 \le x < p : x^4 = \mathrm{eval}(E')^2 \bmod p\}. EE has width L+3L+3 and height H+1H+1.
S(H,2)S(H,2)+string of - of length LL
VH−1V_{H-1}E' (width LL, height HH)
/\|
  1. Operation. EE has the form below, where op is one of +, -, *, /. eval(E)=(eval(E1)opeval(E2)) mod p\mathrm{eval}(E) = (\mathrm{eval}(E_1) \mathrm{op} \mathrm{eval}(E_2)) \bmod p. Here eval(E1)/0=19981204\mathrm{eval}(E_1) / 0 = 19981204. EE has width L1+L2+3L_1+L_2+3 and height HH.
E1 (width L1L_1, height HH)S(H,1)S(H,1)S(⌊H/2⌋,1)S(\lfloor H/2 \rfloor,1)S(H,1)S(H,1)E2 (width L2L_2, height HH)
op
S(⌊(H−1)/2⌋,1)S(\lfloor (H-1)/2 \rfloor,1)
  1. Fraction. EE has the form below, and eval(E)=(eval(E1)/eval(E2)) mod p\mathrm{eval}(E) = (\mathrm{eval}(E_1) / \mathrm{eval}(E_2)) \bmod p. EE has width L+2L+2 and height H1+H2+1H_1+H_2+1.
S(H1,1)S(H_1,1)E1 (width LL, height H1H_1)S(H1,1)S(H_1,1)
string of - of length L+2L+2
S(H2,1)S(H_2,1)E2 (width LL, height H2H_2)S(H2,1)S(H_2,1)

The input is given so that no expression has more than one parse tree under these rules. In all of the above, p=109+7p = 10^9 + 7 is prime.

Input

The first line has the number of rows RR and the number of columns CC. Each of the next RR lines has CC characters, and together they give the expression E0E_0. The width of E0E_0 is CC and its height is RR.

Output

Print eval(E0E_0).

Examples1

  1. Example 1

    Input
    6 10
    /   +--- \
    |   | 6  |
    |   |--- |
    | /\| 3  |
    |--------|
    \ 12 - 9 /
    
    Expected output
    353237869