Evaluation of an Expression

No attempts yetTime limit1sMemory limit128 MB

Problem

You are given an expression EE built from integer constants 00 through 99, variables aa through zz, and the operations of addition, multiplication, and exponentiation by a constant exponent. Remarkably, each of the variables a,b,,za, b, \dots, z appears in EE at most once.

For a given prime number pp, we want to know how many roots the polynomial represented by this expression has modulo pp. In other words, count the number of ways to assign integers from 00 to p1p-1 to the variables in EE so that the value of EE is divisible by pp. Since this number can be large, output it modulo 3001130011.

For example, the expression E=((a+y)(z+8))2E = ((a+y) \cdot (z+8))^2 has 1515 roots modulo p=3p = 3, among which are:

(a=0, y=0, z=0),(a=1, y=2, z=0),(a=2, y=0, z=1)(a=0,\ y=0,\ z=0), \qquad (a=1,\ y=2,\ z=0), \qquad (a=2,\ y=0,\ z=1)

Formally, an expression is defined as follows:

  • Each integer constant 0,1,,90, 1, \dots, 9 is an expression.
  • Each variable a,b,,za, b, \dots, z is an expression.
  • If AA and BB are expressions, then both (A+B) and (A*B) are expressions: the former is the sum of AA and BB, the latter is their product.
  • If AA is an expression and BB is an integer constant from 2,3,,92, 3, \dots, 9, then (A^B) is an expression: it denotes AA raised to the power BB.

Input

The first line contains a prime number pp (2p<150002 \le p < 15000).

The second line contains an expression EE as defined above, given with no whitespace as a string of at most 300300 characters drawn from 09, az, +, *, ^, (, ).

Output

Let kk be the number of roots of the polynomial EE modulo pp. Output the single non-negative integer kmod30011k \bmod 30011.