All problems

Problem 461

Boolean Parenthesization

Hard · 30 pts · String, Dynamic Programming, Interval DP

Problem

A boolean expression alternates symbols and operators: the symbols are T (true) and F (false), and the operators are & (and), | (or) and ^ (xor), as in T|T&F^T. Count the ways to fully parenthesise the expression so that it evaluates to true. Two parenthesisations are different if they group the operations differently. Print the count modulo 1000000007. For each sub-expression, count both the ways to make it true and the ways to make it false, then combine the counts for every operator that could be applied last. Keep the counts in a 64-bit integer type (long long in C++, long in Java) and take the remainder after every addition and multiplication: adding or multiplying two remainders can be larger than a 32-bit int can hold (Python needs nothing special).

Input

A single string: the expression.

Output

Print the number of ways to make the expression true, modulo 1000000007.

Constraints

1 <= number of symbols <= 200

Example input

T|T&F^T

Example output

4

Explanation

T|T&F^T can be grouped in 5 ways, and 4 of them are true, for example ((T|T)&(F^T)) and (T|((T&F)^T)). Only ((T|(T&F))^T) is false, so the answer is 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Shortest Common Supersequence Length Next: Minimum Cost to Cut a Stick