Welcome to ZOJ
Information
Select Problem
Runs
Ranklist
ZOJ Problem Set - 3892
Available Computation Sequence

Time Limit: 1 Second      Memory Limit: 65536 KB

Keine is a teacher and recently she is preparing the test paper of the mid-term test of mathematics. The main content is the vector operation. She has already made some formulas before. Those formulas are about the most common vector operations:

  • Scalar multiplication '*' : number * vector = vector, number * number = number
  • Dot product '.' : vector . vector = number.
  • Cross product '^' : vector ^ vector = vector.

In Gensokyo, there is another amazing operation:

  • Gensokyo product '!': number ! number = number ! vector = vector ! number = vector, vector ! vector = number.
Kamishirasawa_Keine

Unfortunately Keine forgot to add brackets before printing the test paper. What's worse is that she hasn't teached her students the priority of these four operations. So she thinks it's necessary to prepare all possible answers. Now she wants to know the number of available compuation sequences and requires your help.

CAVED!!!!

You don't want to be CAVED, do you?

Input

There are multiple cases.
The first line of input is an integer n (1 ≤ n ≤ 20) which indicates the cases of test data.
The following n lines each contain a nonempty string consisted of '*', '.', '^', '!', lower letters and digits. The length of each string will be less than 105.
The meaning of '*', '.' '^' and '!' are described above. There are at most 100 operators('*', '.', '^', '!'). Consecuive letters represent a vector and consecutive digits represent a number.
It is confirmed that operators are not adjacent. There is always an operator between vector and number.

Output

For each case, please output the number of available computation sequences. It may be very large, so you must output the answer modulo 109 + 7.

Sample Input

4
32*x
x*x
1*x^x
1!x!2!b

Sample Output

1
0
2
5

Hint

The five computation sequences of the 4th sample are listed below:

  1. (((1!x)!2)!b)
  2. ((1!x)!(2!b))
  3. ((1!(x!2))!b)
  4. (1!((x!2)!b))
  5. (1!(x!(2!b)))

If we use permutations to represent computation sequences, then 132 and 312 will be regarded as the same (the 2nd sequence).

Reference

Kamishirasawa Keine


Author: ZHAO, Yueqi
Source: ZOJ Monthly, September 2015
Submit    Status