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 <bits/stdc++.h>
using namespace std;
const int maxn = 404;
int c[maxn][4];
void rotate(int id){
	int p0 = c[id][0];
	for(int j = 0; j < 3; j++) c[id][j] = c[id][j + 1];
	c[id][3] = p0;
}
long long calc(int id){
	long long val = 0;
	for(int k = 0; k < 4; k++) val = val * 1000 + c[id][k];
	return val;
}
long long has[maxn][4];
void ins(int id){
	for(int rot = 0; rot < 4; rot++){
		has[id][rot] = calc(id);
		rotate(id);
	}
	sort(has[id], has[id] + 4);
}
int get_msk(int id, long long vals[4]){
	int i = 0, j = 0, msk = 0;
	while(i < 4 && j < 4){
		if(has[id][i] == vals[j]) msk |= 1<<j, j++;
		else if(has[id][i] < vals[j]) i++;
		else j++;
	}
	return msk;
}
unordered_map<vector<long long>, int, function<size_t(const vector<long long> &v)> > magic(1000000, [](const vector<long long> &v){
	size_t res = 0;
	for(auto e : v) res ^= ((e >> 31) ^ e) & ((1u<<31) - 1);
	return res;
	});
void add(int i, int s){
	set<vector<long long> > can;
	for(int j = 1; j < 1<<4; j++){
		vector<long long> now;
		for(int k = 0; k < 4; k++) if((j>>k)&1){
			if(now.empty() || now.back() != has[i][k]) now.push_back(has[i][k]);
		}
		can.insert(now);
	}
	for(auto e : can) magic[e] += s;
}
long long need[4];
int main(){
	int n; cin >> n;
	for(int i = 0; i < n; i++) for(int j = 0; j < 4; j++) cin >> c[i][j];
	for(int i = 0; i < n; i++){
		ins(i);
		add(i, 1);
	}
	long long ans = 0;
	for(int i = 0; i < n; i++) {
		add(i, -1);
		for(int j = i + 1; j < n; j++){
			add(j, -1);
			for(int rotj = 0; rotj < 4; rotj++){
				int prot[4] = {};
				for(int k = 0; k < 4; k++){
					int nik = (k + 1) % 4;
					int njk0 = (5 - k) % 4;
					int njk1 = (4 - k) % 4; 
					need[k] = 0;
					for(int v : {c[i][nik], c[i][k], c[j][njk0], c[j][njk1]}){
						need[k] = need[k] * 1000 + v;
					}
					c[n][0] = c[i][nik];
					c[n][1] = c[i][k];
					c[n][2] = c[j][njk0];
					c[n][3] = c[j][njk1];
					for(int tk = 0; tk < 4; tk++){
						if(calc(n) == need[k]) prot[k]++;
						rotate(n);
					}
				}
				sort(need, need + 4);
				vector<int> cnt(1<<4);
				for(int k = 1; k < 1<<4; k++){
					vector<long long> now;
					for(int p = 0; p < 4; p++) if((k>>p)&1){
						if(now.empty() || now.back() != need[p]) now.push_back(need[p]);
					}
					cnt[k] = magic[now];
				}
				for(int k = 15; k > 0; k--){
					for(int sk = k; sk; sk = (sk - 1) & k){
						if(sk != k){
							cnt[sk] -= cnt[k];
						}
					}
				}
				vector<long long> f(1<<4, 0), nf(1<<4, 0);
				f[0] = 1;
				for(int k = 1; k < 1<<4; k++){
					if(cnt[k]){
						nf = f;
						for(int sk = k; sk; sk = k & (sk - 1)){
							assert((k&sk) == sk);
							long long ways = 1;
							int bc = __builtin_popcount(sk);
							for(int z = 0; z < bc; z++) ways *= (cnt[k] - z);
							if(ways > 0){
								for(int msk = 0; msk < 1<<4; msk++){
									if((msk&sk) == 0){
										nf[msk|sk] += f[msk] * ways;
									}
								}
							}
						}
						f = nf;
					}
				}	
				for(int k = 0; k < 4; k++) f[15] *= prot[k];
				ans += f[15];
				rotate(j);
			}
			add(j, 1);
		}
		add(i, 1);
	}
	cout << ans / 3 << endl;
	return 0;
}