Ali-Amir wrote numbers in a row. A row is called beautiful if any two of the neighbour numbers in the row have got the same amount of ones in binary or ternary notations.
Ali-Amir wants to count the number of ways all the given numbers can be written in a beautiful row.
The first line contains integer (2 \leq n \leq 20). The next line contains non-negative integers not exceeding 10^9 each.
Output the number of ways all the given numbers can be placed in a beautiful row.
Example 1