eolymp
bolt
Try our new interface for solving problems
Problems

Beautiful row

Beautiful row

Ali-Amir wrote n 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 the all given numbers can be written in a beautiful row.

Input

The first line contains integer n (2n20). The next line contains n non-negative integers not exceeding 109 each.

Output

Output the number of ways the all given numbers can be placed in a beautiful row.

Time limit 2 seconds
Memory limit 244.24 MiB
Input example #1
3
5 1 6
Output example #1
2
Source 2012 VIII Zhautykov Olympiad Almaty, Kazakhstan, January 17