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: Runtime Error)

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()

#ifdef LOCAL
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else 
#define dump(x) true
#endif

constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

template<class T, class U> void chmin(T& t, const U& u) { if (t > u) t = u; }
template<class T, class U> void chmax(T& t, const U& u) { if (t < u) t = u; }

template<class T, class U>
ostream& operator<<(ostream& os, const pair<T, U>& p) {
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator<<(ostream& os, const vector<T>& v) {
	os<<"{";
	rep(i, v.size()) {
		if (i) os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

V<int> cyc(V<int> &a) {
	VV<int> cand;
	rep(j, 4) {
		V<int> vec;
		rep(k, 4) {
			vec.pb(a[(j+k)%4]);
		}
		cand.pb(vec);
	}
	return *min_element(ALL(cand));
}

int com(V<int>& v) {
	int cnt = 0;
	rep(j, 4) {
		V<int> vec;
		rep(k, 4) {
			vec.pb(v[(j+k)%4]);
		}
		if (vec == v) {
			++cnt;
		}
	}
	return cnt;
}

int main() {
	int N; cin >> N;
	VV<int> vf;

	map<V<int>, int> T, coef;

	rep(i, N) {
		V<int> a(4);
		rep(j, 4) cin >> a[j];

		auto vec = cyc(a);
		vf.pb(vec);
		++T[vec];
		dump(vec);
		coef[vec] = com(vec);
	}

	ll ans = 0;

	rep(i, N) {
		rep(j, i) {
			--T[vf[i]];
			--T[vf[j]];

			rep(k, 4) {
				int up[4], dw[4];
				rep(l, 4) {
					up[l] = vf[i][l];
					dw[l] = vf[j][(k-l+4)%4];
				}

				ll t = 1;

				VV<int> use;
				rep(l, 4) {
					V<int> vec{up[l],up[(l+3)%4],dw[(l+3)%4],dw[l]};
					vec = cyc(vec);
					use.pb(vec);
					t *= T[vec] * coef[vec];
					--T[vec];
					if (T[vec] < 0) break;
				}
				ans += t;
				rep(l, 4) ++T[use[l]];
			}

			++T[vf[i]];
			++T[vf[j]];			
		}
	}
	dump(ans);

	cout << ans / 3 << endl;

	return 0;
}