Problem:
Score : 900 points

Problem Statement

AtCoDeer the deer has N square tiles. The tiles are numbered 1 through N, and the number given to each tile is written on one side of the tile. Also, each corner of each tile is painted in one of the 1000 colors, which are represented by the integers 0 between 999. The top-left, top-right, bottom-right and bottom-left corner of the tile with the number i are painted in color C_{i,0}, C_{i,1}, C_{i,2} and C_{i,3}, respectively, when seen in the direction of the number written on the tile (See Figure 1).

Figure 1: The correspondence between the colors of a tile and the input

AtCoDeer is constructing a cube using six of these tiles, under the following conditions:

For each tile, the side with the number must face outward.

For each vertex of the cube, the three corners of the tiles that forms it must all be painted in the same color.

Help him by finding the number of the different cubes that can be constructed under the conditions.
Since each tile has a number written on it, two cubes are considered different if the set of the used tiles are different, or the tiles are used in different directions, even if the formation of the colors are the same. (Each tile can be used in one of the four directions, obtained by 90° rotations.) Two cubes are considered the same only if rotating one in the three dimensional space can obtain an exact copy of the other, including the directions of the tiles.

Figure 2: The four directions of a tile

Constraints

6≦N≦400

0≦C_{i,j}≦999 (1≦i≦N , 0≦j≦3)

Input

The input is given from Standard Input in the following format:

N
C_{1,0} C_{1,1} C_{1,2} C_{1,3}
C_{2,0} C_{2,1} C_{2,2} C_{2,3}
:
C_{N,0} C_{N,1} C_{N,2} C_{N,3}

Output

Print the number of the different cubes that can be constructed under the conditions.

Sample Input 1

6
0 1 2 3
0 4 6 1
1 6 7 2
2 7 5 3
6 4 5 7
4 0 3 5

Sample Output 1

1

The cube below can be constructed.

Sample Input 2

8
0 0 0 0
0 0 1 1
0 1 0 1
0 1 1 0
1 0 0 1
1 0 1 0
1 1 0 0
1 1 1 1

Sample Output 2

144

Sample Input 3

6
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Sample Output 3

122880

Solution in C++14 (GCC 5.4.1) (judge verdict: Time Limit Exceeded)

#include <stdio.h>
#include <bits/stdc++.h>

 
#define REP(i,n) for(int(i)=0;(i)<(int)(n);(i)++)

#include <vector>
#include <utility>


#include <algorithm>


int n;
int c[410][4];
int tgt[4][4];
long long dp[16];
int can[4];

bool ok(int from, int dest) {
	for (int i = 0; i < 4; i++) if (c[from][i] != tgt[dest][i]) return false;
	return true;
}

void rotate(int idx) {
	int t = c[idx][3];
	for (int i = 2; i >= 0; i--) c[idx][i+1] = c[idx][i];
	c[idx][0] = t;
}

void solve() {
	scanf("%d", &n);
	REP(i,n) REP(j,4) scanf("%d", &c[i][j]);
	
	long long ans = 0;

	REP(front, n) {
		for (int back = 0; back < front; back++) {
			for (int ro = 0; ro < 4; ro++) {

				tgt[0][0] = c[front][0];
				tgt[0][1] = c[back][1];
				tgt[0][2] = c[back][0];
				tgt[0][3] = c[front][1];

				tgt[1][0] = c[front][1];
				tgt[1][1] = c[back][0];
				tgt[1][2] = c[back][3];
				tgt[1][3] = c[front][2];

				tgt[2][0] = c[front][3];
				tgt[2][1] = c[front][2];
				tgt[2][2] = c[back][3];
				tgt[2][3] = c[back][2];

				tgt[3][0] = c[back][1];
				tgt[3][1] = c[front][0];
				tgt[3][2] = c[front][3];
				tgt[3][3] = c[back][2];

				/*if (front == 4 && back == 0) {
					REP(i,4) {
						REP(j,4) printf("%d ", tgt[i][j]);
						printf("\n");
					}
					printf("\n");
				}*/

				REP(bm,16) dp[bm] = 0;
				dp[0]=1;

				for (int ot = 0; ot < front; ot++) if (ot != back) {
					REP(i,4) can[i] = 0;
					for (int rr = 0; rr < 4; rr++) {
						for (int i = 0; i < 4; i++) can[i] += ok(ot, i);
						rotate(ot);
					}

					for (int bm = 15; bm >= 0; bm--) {
						for (int bit = 0; bit < 4; bit++) if ((1<<bit)&bm) {
							dp[bm] += dp[bm ^ (1<<bit)] * can[bit];
						}
					}
				}

				ans += dp[15];
				rotate(back);
			}
		}
	}

	printf("%lld\n", ans);
}


int main() {
    solve();
}