Bob is playing with array expressions!
Bob has (
) arrays, denoted as
,
,
,
, and each array has
integers. The index of each array goes from
to
.
Bob defines two operations between arrays:
means:
for all
from
to
.
means:
for all
from
to
.
These operators have equal precedence, and parentheses ()
can be used to control evaluation order.
Bob writes down an array expression , where each operand is one of the
arrays, the operator is either
<
or >
. Bob may add some parentheses, (
and )
, to change the precedence. The result of the expression will be a new array with
integers.
However, Alice comes along and changes some of the <
or >
operators into ?
, which means the operator could be either <
or >
. If Alice changes operators to
?
, the expression will have possible evaluations, each resulting in a different array.
Your task is to evaluate all possible arrays and compute the sum of all their elements, over all possible expressions. Since the result can be very large, print it modulo
Input Specification
The first line of the input contains two integers, and
, (
,
) the length of each array and the number of arrays.
Each of the following lines contains
integers,
(
,
,
), indicating the
th integer in the
-th array.
The last line contains one string , (
), the array expression.
will only contain the following characters: the digit
to
,
(
, )
, <
, >
, and ?
. The digit represents the index of the array. For example, indicates
.
Output Specification
Output one integer, the sum of all possible result arrays .
Constraints
Subtask | Points | Additional constraints |
---|---|---|
( , ) or ? in | ||
? in | ||
( or ) in | ||
? in | ||
? in | ||
No additional constraints. |
Sample Input 1
2 3
3 1
2 2
2 3
1>2?0
Sample Output 1
9
Explanation for Sample 1
There are two possible result arrays:
, the result is
, the result is
Thus, the total sum is .
Sample Input 2
3 3
4 3 2
2 3 1
2 3 3
1?0>2?0
Sample Ouput 2
36
Sample Input 3
5 3
354 321 414 205 257
458 996 554 635 730
681 374 903 966 349
2<0>2<0>(1>2)>(0<0)
Sample Output 3
4276
Comments