Bessie Goes Moo

No attempts yetTime limit1sMemory limit256 MB

Problem

Farmer John and Bessie the cow trade math puzzles whenever they have free time. The last puzzle John gave Bessie was hard enough that she could not solve it, so now she wants to hand John a hard puzzle of her own.

Bessie gives John the expression (B+E+S+S+I+E)(G+O+E+S)(M+O+O)(B+E+S+S+I+E)(G+O+E+S)(M+O+O), which uses the seven variables BB, EE, SS, II, GG, OO, MM. The OO is a variable, not a zero. For each variable Bessie lists up to 500 integer values that the variable is allowed to take. John has to count how many different assignments of values to the variables make the whole expression a multiple of 7.

The answer does not always fit in a 32-bit integer, so use a 64-bit integer type.

Input

The first line contains an integer NN. Each of the next NN lines contains a variable name and one value that variable is allowed to take. Each variable appears at least once and at most 500 times, so 7N35007 \le N \le 3500. The same value is never listed twice for the same variable. Every value is in the range 105-10^5 to 10510^5.

Output

Print one integer, the number of assignments of values to the variables that make the expression a multiple of 7.

Hint

In the first example the two assignments that count are

(B,E,S,I,G,O,M) = (2, 5, 7, 9,  1, 16, 19) -> 51,765
                = (2, 5, 7, 9,  1, 16, 2 ) -> 34,510