#P7324. [WC2021] 表达式求值

[WC2021] 表达式求值

Description

Define the binary operator <: for two arrays A,BA, B of length nn (indices from 11 to nn), the result of AA<BB is also an array of length nn, denoted by CC. Then C[i]=min(A[i],B[i])C[i] = \min(A[i], B[i]) (1in1 \le i \le n).

Define the binary operator >: for two arrays A,BA, B of length nn (indices from 11 to nn), the result of AA>BB is also an array of length nn, denoted by CC. Then C[i]=max(A[i],B[i])C[i] = \max(A[i], B[i]) (1in1 \le i \le n).

Now there are mm (1m101 \le m \le 10) integer arrays A0,A1,,Am1A_0, A_1, \ldots , A_{m-1}, all of length nn. You are given an expression EE to evaluate. Every operand in EE is one of A0,A1,,Am1A_0, A_1, \ldots , A_{m-1}, and EE contains only the two operators < and > (they have the same precedence). Therefore, the value of this expression is also an array of length nn.

In particular, the expression EE may also contain the operator ?, which means this operator may be < or >. Thus, if the expression contains tt ? characters, it can generate 2t2^t fully determined expressions, producing 2t2^t results (each result is an array). Your task is to compute the sum of all elements across these 2t2^t result arrays. You only need to output this total sum modulo 109+7{10}^9 + 7.

Input Format

The first line contains two integers n,mn, m, representing the array length and the number of arrays.

Lines 22 to m+1m + 1 each contain nn integers separated by spaces. The jj-th number on line ii represents Ai2[j]A_{i-2}[j] (2im+12 \le i \le m + 1, 1jn1 \le j \le n).

The last line contains a string SS, representing the expression EE. SS contains only the characters 0 to 9, (, ), <, >, ?. A digit character indicates the index of an operand; for example, character 2 means the operand is A2A_2.

Output Format

Output a single integer: the sum of all elements in the results of the 2t2^t expressions, modulo 109+7{10}^9 + 7.

2 3
3 1
2 2
2 3
1>2?0

9

3 3
4 3 2
2 3 1
2 3 3
1?0>2?0

36

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)

4276

见附件中的 expr/expr4.in
见附件中的 expr/expr4.ans

Hint

Sample Explanation #1

The expressions generated by EE are:

  1. A1A_1>A2A_2<A0A_0, whose result is [2,1][2, 1].
  2. A1A_1>A2A_2>A0A_0, whose result is [3,3][3, 3].

So the answer is 2+1+3+3=92 + 1 + 3 + 3 = 9.

Constraints

For all test points: 1n5×1041 \le n \le 5 \times {10}^4, 1m101 \le m \le 10, S5×104|S| \le 5 \times {10}^4, 1Ai[j]1091 \le A_i[j] \le {10}^9.

The specific limits for each test point are shown in the table below:

Test Point ID nn \le E\vert E \vert \le Special Restrictions
141 \sim 4 55 1010 SS does not contain parentheses or question marks
575 \sim 7 1010 100100 SS does not contain question marks
898 \sim 9 22 50005000 SS does not contain parentheses
101110 \sim 11 None
121412 \sim 14 50005000 SS does not contain question marks
151715 \sim 17 5×1045 \times {10}^4
182018 \sim 20 None

Translated by ChatGPT 5