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: Accepted)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define Would
#define you
#define please

typedef tuple<int, int, int, int> TP;
TP narabi(int a, int b, int c, int d) {
	TP kari[4];
	kari[0] = tie(a, b, c, d);
	kari[1] = tie(b, c, d, a);
	kari[2] = tie(c, d, a, b);
	kari[3] = tie(d, a, b, c);
	sort(kari, kari + 4);
	return kari[0];
}

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);


	int N;
	cin >> N;
	TP C[400];
	map<TP, int> MP;
	rep(i, N) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		C[i] = narabi(a, b, c, d);
		MP[C[i]]++;
	}

	ll kotae = 0;
	rep(i, N) rep(j, N) if (i != j) {
		MP[C[i]]--;
		MP[C[j]]--;

		int a, b, c, d, e, f, g, h;
		a = get<0>(C[i]);
		b = get<1>(C[i]);
		c = get<2>(C[i]);
		d = get<3>(C[i]);
		e = get<0>(C[j]);
		f = get<1>(C[j]);
		g = get<2>(C[j]);
		h = get<3>(C[j]);

		rep(x, 4) {
			TP D[4];
			D[0] = narabi(a, d, f, e);
			D[1] = narabi(a, e, h, b);
			D[2] = narabi(b, h, g, c);
			D[3] = narabi(c, g, f, d);
			ll kari = 1;
			rep(k, 4) {
				kari *= MP[D[k]]--;
				if (get<0>(D[k]) == get<2>(D[k]) && get<1>(D[k]) == get<3>(D[k])) {
					kari *= 2;
					if (get<1>(D[k]) == get<2>(D[k])) kari *= 2;
				}
				if (kari < 0) kari = 0;
			}
			rep(k, 4) MP[D[k]]++;
			kotae += kari;

			int tmp = e;
			e = f;
			f = g;
			g = h;
			h = tmp;
		}

		MP[C[i]]++;
		MP[C[j]]++;
	}
	co(kotae / 6);

	Would you please return 0;
}