跳到主要内容
图灵 OJTURING / ONLINE JUDGE

#F1544. Boolean Function

    ID: 1550 传统题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 10 上传者: 标签>位运算动态规划表达式解析数学字符串挑战难度共享题库Codeforces英文题面题目来源题面语言

Boolean Function

题目描述

E. Boolean Function
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 109+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≤24) − 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 ai,bi,ci,di,ei (0≤ai,bi,ci,di,ei≤1), separated by spaces and meaning that f(ai,bi,ci,di)=ei.

It is guaranteed that all the tuples (ai,bi,ci,di) are distinct.

Output

In a single line print the answer to the problem.

Examples
Input
?
2
1 0 1 0 1
0 1 1 0 1
Output
2
Input
(A)?(?)
1
1 1 0 0 0
Output
4
Input
((?)&(?))|((?)&(?))
0
Output
4096
Input
b
1
1 0 1 1 1
Output
1
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)'.


题目来源:fps-www.educg.net-codeforce-1-2833.xml.zip;FPS 共享题包,题包内第 1063 题。保留原作者与原赛事署名。