Binary cryptarithm decryption

Given a short cipher string where letters replace some characters of an unknown binary equation, count how many valid equations from the given grammar match it.

Hard9BacktrackingDynamic programmingStringCombinatoricsNo attempts yetTime limit2sMemory limit512 MB

Problem

An arithmetic equation written with binary numbers has been encrypted. Count how many original equations the cipher text could have come from.

The original equation uses only the binary digits 0 and 1, the operator symbols +, -, *, the parentheses ( and ), and the equal sign =. The encryption replaces some occurrences of characters in the equation by letters. Every replaced occurrence of one character uses the same letter, and two different characters are never replaced by the same letter. The encryption need not replace every occurrence of a character, so one occurrence may become a letter while another stays as it is, and some characters may never be replaced at all. Any Roman letter, lowercase or uppercase, may be used as a replacement, and case is significant, so a and A are different letters. Operator symbols, parentheses and the equal sign can be replaced just like digits.

The equation is derived from the start symbol Q of the following context-free grammar.

Q ::= E=E
E ::= T | E+T | E-T
T ::= F | T*F
F ::= N | -F | (E)
N ::= 0 | 1B
B ::= ε | 0B | 1B

Here ε means the empty string.

As the grammar shows, an equation has exactly one equal sign, and each side is an expression built from numbers, addition, subtraction, multiplication and negation. Multiplication is computed before addition and subtraction, and negation is computed before multiplication. Multiplication, addition and subtraction are left associative, so x-y+z means (x-y)+z, not x-(y+z). Numbers are written in binary as a sequence of 0 and 1, and a number of two or more digits always starts with 1. Parentheses and negations may appear redundantly, as in ((--x+y))+z.

Given a cipher text, count how many correct equations can be encrypted into it. An equation is correct when it conforms to the grammar and both sides have the same value.

For the cipher text ACM, C must be =, because an equation has one = between two expressions. A and M are different letters, so they stand for different characters, and the two single-character sides can never have the same value. For the cipher text icpc, the only correct equation is -0=0. For the cipher text BAYL0R, three different correct equations exist: 0=-(0), 0=(-0) and -0=(0). Note that one of the two occurrences of 0 was left unreplaced in the cipher text.

Input

The first line holds the cipher text. It is a string of Roman letters, binary digits, operator symbols, parentheses and equal signs, and its length is between 1 and 31 characters.

Output

Print in one line the number of correct equations that can be encrypted into the given string.