시간 제한메모리 제한제출정답맞힌 사람정답 비율
3 초 256 MB30201872.000%

문제

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.

입력

The first line of input file contains integer N (2 ≤ N ≤ 20). The next line contains N non-negative integers not exceeding 109 each.

출력

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

예제 입력 1

3
5 1 6

예제 출력 1

2

힌트

In the sample 5 = 123 and 1 = 13, 5 = 1012 and 6 = 1102, thus rows 1 5 6 and 6 5 1 are beautiful.