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: Wrong Answer)

#include<iostream>
#include<iomanip>
#include<map>
#include<unordered_map>
#include<set>
#include<unordered_set>
#include<vector>
#include<array>
#include<string>
#include<stack>
#include<queue>
#include<algorithm>
#include<cassert>
#include<functional>
#include<random>
#include<complex>
#include<bitset>
//#include<boost/multiprecision/cpp_int.hpp>
#define int int64_t
#define uint uint64_t
#define REP(i, a, b) for (int64_t i = (int64_t)(a); i < (int64_t)(b); i++)
#define rep(i, a) REP(i, 0, a)
#define EACH(i, a) for (auto i: a)
#define ITR(x, a) for (auto x = a.begin(); x != a.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define HAS(a, x) (a.find(x) != a.end())
#define Min(x) *min_element(ALL(x))
#define Max(x) *max_element(ALL(x))
#define Unique(L) (L.erase(unique(ALL(L)), L.end()))
#define veccat(v1, v2) std::copy((v2).begin(),(v2).end(),std::back_inserter(v1))
#define intmax (std::numeric_limits<int64_t>::max() / 4)
using namespace std;
//typedef boost::multiprecision::cpp_int bigint;
const double EPS = 1e-9;
const double PI = acos(-1.0);

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

	int N;
	cin >> N;
	vector<array<int, 4>>c(N);
	rep(i, N)rep(j, 4)cin >> c[i][j];
	map<int, vector<int>>m1;
	map<array<int, 4>, vector<array<int, 2>>>m4;
	rep(i, N) {
		if (c[i][0] == c[i][1] && c[i][1] == c[i][2] && c[i][2] == c[i][3]) {
			m1[c[i][0]].push_back(i);
		}
		else {
			rep(j, 4) {
				m4[{ c[i][j], c[i][(j + 1) % 4], c[i][(j + 2) % 4], c[i][(j + 3) % 4] }].push_back({ i,j });
			}
		}
	}

	int ans = 0;
	rep(i, N)REP(j, i + 1, N)rep(k, 4) {
		array<int, 4>a1 = { c[i][0],c[j][(k + 1) % 4],c[j][(k + 0) % 4],c[i][1] };
		array<int, 4>a2 = { c[i][1],c[j][(k + 0) % 4],c[j][(k + 3) % 4],c[i][2] };
		array<int, 4>a3 = { c[i][2],c[j][(k + 3) % 4],c[j][(k + 2) % 4],c[i][3] };
		array<int, 4>a4 = { c[i][3],c[j][(k + 2) % 4],c[j][(k + 1) % 4],c[i][0] };
		for (auto x1 : m4[a1])if (i < x1[0] && j != x1[0]) {
			for (auto x2 : m4[a2])if (i < x2[0] && j != x2[0] && x1[0] != x2[0]) {
				for (auto x3 : m4[a3])if (i < x3[0] && j != x3[0] && x1[0] != x3[0] && x2[0] != x3[0]) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1[0] != x4[0] && x2[0] != x4[0] && x3[0] != x4[0]) {
						ans++;
					}
					for(auto x4:m1[a4[0]])if (i < x4 && j != x4 && x1[0] != x4 && x2[0] != x4 && x3[0] != x4) {
						ans++;
					}
				}
				for(auto x3:m1[a3[0]])if (i < x3 && j != x3 && x1[0] != x3 && x2[0] != x3) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1[0] != x4[0] && x2[0] != x4[0] && x3 != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1[0] != x4 && x2[0] != x4 && x3 != x4) {
						ans++;
					}
				}
			}
			for(auto x2:m1[a2[0]])if (i < x2 && j != x2 && x1[0] != x2) {
				for (auto x3 : m4[a3])if (i < x3[0] && j != x3[0] && x1[0] != x3[0] && x2 != x3[0]) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1[0] != x4[0] && x2 != x4[0] && x3[0] != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1[0] != x4 && x2 != x4 && x3[0] != x4) {
						ans++;
					}
				}
				for (auto x3 : m1[a3[0]])if (i < x3 && j != x3 && x1[0] != x3 && x2 != x3) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1[0] != x4[0] && x2 != x4[0] && x3 != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1[0] != x4 && x2 != x4 && x3 != x4) {
						ans++;
					}
				}
			}
		}
		for(auto x1:m1[a1[0]])if (i < x1 && j != x1) {
			for (auto x2 : m4[a2])if (i < x2[0] && j != x2[0] && x1 != x2[0]) {
				for (auto x3 : m4[a3])if (i < x3[0] && j != x3[0] && x1 != x3[0] && x2[0] != x3[0]) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1 != x4[0] && x2[0] != x4[0] && x3[0] != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1 != x4 && x2[0] != x4 && x3[0] != x4) {
						ans++;
					}
				}
				for (auto x3 : m1[a3[0]])if (i < x3 && j != x3 && x1 != x3 && x2[0] != x3) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1 != x4[0] && x2[0] != x4[0] && x3 != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1 != x4 && x2[0] != x4 && x3 != x4) {
						ans++;
					}
				}
			}
			for (auto x2 : m1[a2[0]])if (i < x2 && j != x2 && x1 != x2) {
				for (auto x3 : m4[a3])if (i < x3[0] && j != x3[0] && x1 != x3[0] && x2 != x3[0]) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1 != x4[0] && x2 != x4[0] && x3[0] != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1 != x4 && x2 != x4 && x3[0] != x4) {
						ans++;
					}
				}
				for (auto x3 : m1[a3[0]])if (i < x3 && j != x3 && x1 != x3 && x2 != x3) {
					for (auto x4 : m4[a4])if (i < x4[0] && j != x4[0] && x1 != x4[0] && x2 != x4[0] && x3 != x4[0]) {
						ans++;
					}
					for (auto x4 : m1[a4[0]])if (i < x4 && j != x4 && x1 != x4 && x2 != x4 && x3 != x4) {
						ans++;
					}
				}
			}
		}
	}
	cout << ans << endl;

	return 0;
}