Hex statistics

For every digit of a hexadecimal string S, find the smallest, largest, and sum over all 16! deletion orders of the total of the 16 prefix sums.

Medium7MathCombinatoricsSortingNo attempts yetTime limit1sMemory limit512 MB

Problem

A sequence SS holds NN integers written in hexadecimal. Write sum(S)\mathrm{sum}(S) for the sum of all elements of SS.

Let pp be a permutation of the 16 hexadecimal digits. Deleting a digit dd means erasing every occurrence of dd from every element of SS. The digits that remain keep their order and are read as a hexadecimal number again, and an element that loses all of its digits becomes 0. A deletion can leave a 0 in front, which does not change the value.

Delete the digits one at a time in the order given by pp and record the sequence after each deletion. That gives 16 sequences. Writing S[d1,,dk]S[d_1,\dots,d_k] for the result of deleting the digits d1d_1 through dkd_k from SS, the 16 sequences are S[p1]S[p_1], S[p1,p2]S[p_1,p_2], \dots, S[p1,,p16]S[p_1,\dots,p_{16}], and every element of the last one is 0. Define

total(S,p)=sum(S[p1])+sum(S[p1,p2])++sum(S[p1,,p16])\mathrm{total}(S,p) = \mathrm{sum}(S[p_1]) + \mathrm{sum}(S[p_1,p_2]) + \dots + \mathrm{sum}(S[p_1,\dots,p_{16}])

For example, take S=[9af47c0b,2545557,ff6447979]S = [\texttt{9af47c0b}, \texttt{2545557}, \texttt{ff6447979}] and a permutation pp that starts with 4\texttt{4}, 9\texttt{9}, 5\texttt{5}. Deleting the digit 4\texttt{4} gives S[4]=[9af7c0b,255557,ff67979]S[\texttt{4}] = [\texttt{9af7c0b}, \texttt{255557}, \texttt{ff67979}], and deleting the digit 9\texttt{9} after that gives S[4,9]=[af7c0b,255557,ff677]S[\texttt{4},\texttt{9}] = [\texttt{af7c0b}, \texttt{255557}, \texttt{ff677}].

The value of total(S,p)\mathrm{total}(S,p) depends on which permutation pp is used. Over all 16!16! permutations of the hexadecimal digits, compute three values: the smallest total, the largest total, and the sum of the totals of every permutation. Print the third value modulo 3b9aca07, which is 109+710^9+7 in decimal. The first two are printed exactly, with no modulo taken.

Input

The first line contains the length NN of the sequence. Each of the next NN lines contains one element PP of the sequence SS, in input order. Every number in the input, including NN, is written in hexadecimal with lowercase letters.

Constraints

  • 1N3f\texttt{1} \le N \le \texttt{3f}
  • 0Pfffffffff\texttt{0} \le P \le \texttt{fffffffff}

Output

Print one line with three integers separated by single spaces, all in hexadecimal with lowercase letters: the smallest total, the largest total, and the sum of the totals of all 16!16! permutations modulo 3b9aca07.