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<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
typedef pair<ll, ll> LP;
typedef pair<ld, ld> LDP;
typedef unsigned long long ul;
int n;
ll t1000[4];
void init() {
	t1000[0] = 1;
	rep(i, 3) {
		t1000[i + 1] = t1000[i] * 1000;
	}
}
ll calc(ll a, ll b, ll c, ll d) {
	ll res = INF;
	vector<ll> v = { a,b,c,d };
	rep(j, 4) {
		ll sum = 0;
		rep(k, 4) {
			sum += v[k] * t1000[k];
		}
		res = min(res, sum);
		v.push_back(v[0]);
		v.erase(v.begin());
	}
	return res;
}
ll c[400];
ll turn(ll x) {
	vector<ll> v;
	per(j, 4) {
		v.push_back(x / t1000[j]);
		x %= t1000[j];
	}
	reverse(v.begin(), v.end());
	v.push_back(v[0]); v.erase(v.begin());
	ll res = 0;
	rep(j, 4) {
		res += v[j] * t1000[j];
	}
	return res;
}
vector<ll> query(ll k) {
	vector<ll> res;
	per(j, 4) {
		res.push_back(k / t1000[j]);
		k %= t1000[j];
	}
	reverse(res.begin(), res.end());
	return res;
}
map<ll, int> mp,num;
int main() {
	init();
	cin >> n;
	rep(i, n) {
		ll x, y, z, w; cin >> x >> y >> z >> w;
		c[i] = calc(x, y, z, w);
		mp[c[i]]++;
		ll t = c[i];
		int cnt = 0;
		rep(j, 4) {
			t = turn(t);
			if (t == c[i])cnt++;
		}
		num[c[i]] = cnt;
		//cout << c[i] << endl;
	}
	ll ans = 0;
	rep(i, n) {
		rep(j, n) {
			if (i == j)continue;
			rep(k, n) {
				if (i == k || j == k)continue;
				vector<ll> a1 = query(c[i]);
				ll t1 = c[j], t2 = c[k];
				rep(l, 4) {
					t1 = turn(t1);
					vector<ll> a2 = query(t1);
					if (a1[0] != a2[1] || a1[3] != a2[2])continue;
					rep(m, 4) {
						t2 = turn(t2);
						vector<ll> a3 = query(t2);
						if (a1[1] != a3[0] || a1[2] != a3[3])continue;
						//cout << t1 << " " << t2 << endl;
						ll z[3];
						z[0] = calc(a3[1], a2[0], a2[3], a3[2]);
						z[1] = calc(a2[2], a3[3], a3[2], a2[3]);
						z[2] = calc(a2[0], a3[1], a3[0], a2[1]);
						ll c1 = mp[z[0]];
						//cout << z[0] << endl;
						//cout << c1 << endl;
						if (z[0] == c[i])c1--;
						if (z[0] == c[j])c1--;
						if (z[0] == c[k])c1--;
						ll c2 = mp[z[1]];
						//cout << c2 << endl;
						if (z[1] == c[i])c2--;
						if (z[1] == c[j])c2--;
						if (z[1] == c[k])c2--;
						if (z[0] == z[1])c2--;
						ll c3 = mp[z[2]];
						//cout << c3 << endl;
						if (z[2] == c[i])c3--;
						if (z[2] == c[j])c3--;
						if (z[2] == c[k])c3--;
						if (z[0] == z[2])c3--;
						if (z[1] == z[2])c3--;
						c1 *= num[z[0]];
						c2 *= num[z[1]];
						c3 *= num[z[2]];
						//cout << c1 << " " << c2 << " " << c3 << endl;
						ans += c1 * c2*c3;
					}
				}
			}
		}
	}
	//cout << ans << endl;
	cout << ans / 6 << endl;
	//stop
	return 0;
}