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), which uses the seven variables B, E, S, I, G, O, M. The O 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.
The first line contains an integer N. Each of the next N 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 7≤N≤3500. The same value is never listed twice for the same variable. Every value is in the range −105 to 105.
Print one integer, the number of assignments of values to the variables that make the expression a multiple of 7.
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