#P7088. [NWRRC 2013] J

[NWRRC 2013] J

题目描述

The JJ programming language, developed in the early 1990s1990s by Kenneth EE . Iverson and Roger Hui, is a synthesis of APL (also by Iverson) and the FP and FL function-level languages created by John Backus.

Wikipedia. JJ (programming language)

APL language family is famous for its support of advanced operations on vectors and arrays, and JJ is not an exception. For example, all unary and binary numeric operations in these languages by default are applicable to vectors and arrays of different dimensions. Plus operation (+)(‘+') can add not only scalars, like in other languages, but also scalars and vectors (the scalar is added to each component of the vector), or vectors and vectors (the vectors are added component-wise).

The expressive power of JJ is amazing (as well as its cryptic syntax), but for this problem we need just a small subset of the language. We consider a single expression, where we may use one vector variable XX , one scalar variable NN -- the length of the vector XX , and the following operations:

We can add (+),(‘+'), subtract ()(‘-') or multiply (×)(‘ \times ') two vectors, vector and scalar, or two scalars.

We can use unary minus ()(‘-') and unary squaring operations (×:)(‘ \times :') for scalars and vectors (componentwise).(component-wise).

We can fold a vector with plus operation (+/)(‘+/') -- that is, compute the sum of a vector (unary operation).

Operations are evaluated right-to-left, natural precedence of operations is ignored in JJ . The order of evaluation can be altered by parentheses. More precisely the syntax is specified in the following BNF.

$〈expressio_n〉 ::= 〈ter_m〉 | 〈ter_m〉 (‘+' | ‘-' | ‘ \times ') 〈expressio_n〉 | (‘-' | ‘ \times :' | ‘+/') 〈expressio_n〉$

$〈ter_m〉 ::= ‘('〈expressio_n〉‘)' | ‘X' | ‘N' | 〈number〉$

number::=(01〈number〉 ::= (‘0' | ‘1' | . . . 9)+| ‘9')^{+}

To correctly impose one more limitation on expression syntax, let us define complexity of an expression:

complexity of scalars (numbers, N,‘N', and result of fold) is zero;

complexity of X‘X' is one;

complexity of addition and subtraction is the maximum of their operands' complexities;

complexity of multiplication is the sum of its operands' complexities;

complexity of unary squaring is twice the complexity of its operand.

For example, the complexity of expression (3+/×:×:X)X××:X`(3-+/ \times : \times :X)-X \times \times :X` is 33 , while the complexity of its subexpression ×:×:X` \times : \times :X` is 44 .

Your program is given a scalar-valued expression and a value of the vector XX , and it should compute the expression result modulo 109.10^{9}. The complexity of each subexpression in the given expression does not exceed 1010 .

输入格式

The first line contains one integer number N(1N105)N (1 \le N \le 10^{5}) -- the length of the vector XX .

The second line contains NN integers -- components of the vector X(0Xi<109).X (0 \le X_{i} < 10^{9}).

The third line contains the expression to be computed, a non-empty string of not more than 105105 symbols. Each number in the expression is less than 109109 . The fold is never applied to a scalar.

输出格式

Output a single integer number -- the expression result modulo 109.10^{9}.

5
1 2 3 4 5
+/*:X

55

5
1 2 3 4 5
N++/X-X+1

0

3
11 56 37
+/(3-+/*:*:X)-X**:X

964602515

提示

Time limit: 2 s, Memory limit: 256 MB.