Codeforces Round 323 (Div. 1)
E
Boolean Function
Codeforces
3000
原题
← D→
翻译中…

In this problem we consider Boolean functions of four variables $*A*, *B*, *C*, *D*$. Variables $*A*, *B*, *C*$ and $*D*$ are logical and can take values 0 or 1. We will define a function using the following grammar:

翻译中…

<expression> ::= <variable> | (<expression>) <operator> (<expression>)

翻译中…

<variable> ::= 'A' | 'B' | 'C' | 'D' | 'a' | 'b' | 'c' | 'd'

翻译中…

<operator> ::= '&' | '|'

翻译中…

Here large letters $*A*, *B*, *C*, *D*$ represent variables, and small letters represent their negations. For example, if $*A* = 1$, then character 'A' corresponds to value 1, and value character 'a' corresponds to value 0. Here character '&' corresponds to the operation of logical AND, character '|' corresponds to the operation of logical OR.

翻译中…

You are given expression $*s*$, defining function $*f*$, where some operations and variables are missing. Also you know the values of the function $*f*(*A*, *B*, *C*, *D*)$ for some $*n*$ distinct sets of variable values. Count the number of ways to restore the elements that are missing in the expression so that the resulting expression corresponded to the given information about function $*f*$ in the given variable sets. As the value of the result can be rather large, print its remainder modulo $10^{9} + 7$.

翻译中…

Input

翻译中…

The first line contains expression $*s*$ ($1 ≤ |*s*| ≤ 500$), where some characters of the operators and/or variables are replaced by character '?'.

翻译中…

The second line contains number $*n*$ ($0 ≤ *n* ≤ 2^{4}$) — the number of integers sets for which we know the value of function $*f*(*A*, *B*, *C*, *D*)$. Next $*n*$ lines contain the descriptions of the sets: the $*i*$-th of them contains five integers $*a*_{*i*}, *b*_{*i*}, *c*_{*i*}, *d*_{*i*}, *e*_{*i*}$ ($0 ≤ *a*_{*i*}, *b*_{*i*}, *c*_{*i*}, *d*_{*i*}, *e*_{*i*} ≤ 1$), separated by spaces and meaning that $*f*(*a*_{*i*}, *b*_{*i*}, *c*_{*i*}, *d*_{*i*}) = *e*_{*i*}$.

翻译中…

It is guaranteed that all the tuples ($*a*_{*i*}, *b*_{*i*}, *c*_{*i*}, *d*_{*i*}$) are distinct.

翻译中…

Output

翻译中…

In a single line print the answer to the problem.

翻译中…

Note

翻译中…

In the first sample the two valid expressions are 'C' and 'd'.

翻译中…

In the second sample the expressions look as follows: '(A)&(a)', '(A)&(b)', '(A)&(C)', '(A)&(D)'.

翻译中…

样例

Input #1
?
2
1 0 1 0 1
0 1 1 0 1
Output #1
2
InputCopy OutputCopy
Input #2
(A)?(?)
1
1 1 0 0 0
Output #2
4
InputCopy OutputCopy
Input #3
((?)&(?))|((?)&(?))
0
Output #3
4096
InputCopy OutputCopy
Input #4
b
1
1 0 1 1 1
Output #4
1
InputCopy OutputCopy
题目信息
题号
582E
时间限制
4 s
内存限制
256 MB
标签
位运算
动态规划
表达式解析

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。