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 <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
#include <iomanip>
using namespace std;
#define pb push_back
#define mp make_pair
typedef pair<int,int> pii;
typedef long long ll;
typedef double ld;
typedef vector<int> vi;
#define fi first
#define se second
#define fe first
#define FO(x) {freopen(#x".in","r",stdin);freopen(#x".out","w",stdout);}
#define Edg int M=0,fst[SZ],vb[SZ],nxt[SZ];void ad_de(int a,int b){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;}void adde(int a,int b){ad_de(a,b);ad_de(b,a);}
#define Edgc int M=0,fst[SZ],vb[SZ],nxt[SZ],vc[SZ];void ad_de(int a,int b,int c){++M;nxt[M]=fst[a];fst[a]=M;vb[M]=b;vc[M]=c;}void adde(int a,int b,int c){ad_de(a,b,c);ad_de(b,a,c);}
#define es(x,e) (int e=fst[x];e;e=nxt[e])
#define esb(x,e,b) (int e=fst[x],b=vb[e];e;e=nxt[e],b=vb[e])
typedef pair<pii,pii> s4;
int n,c[555][4],cc[555]; s4 t[555][4],g[555];
#define S 5
map<s4,ll> ms[S];
ll f[17];
#define p4(a,b,c,d) make_pair(pii(a,b),pii(c,d))
s4 py(s4 x)
{
	return min(min(x,p4(x.fi.se,x.se.fi,x.se.se,x.fi.fi)),
	min(p4(x.se.fi,x.se.se,x.fi.fi,x.fi.se),
	p4(x.se.se,x.fi.fi,x.fi.se,x.se.fi)));
}
ll fac[S];
void add(int x)
{
	for(int j=S-1;j>1;--j)
		ms[j][g[x]]+=ms[j-1][g[x]]*cc[x];
	ms[1][g[x]]+=cc[x];
}
void del(int x)
{
	ms[1][g[x]]-=cc[x];
	for(int j=1;j<S;++j)
		ms[j][g[x]]-=ms[j-1][g[x]]*cc[x];
}
int main()
{
	fac[0]=1; ll aa=0;
	for(int i=1;i<S;++i) fac[i]=fac[i-1]*i;
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		for(int j=0;j<4;++j) scanf("%d",&c[i][j]);
	for(int x=1;x<=n;++x)
	{
		for(int i=0;i<4;++i)
			t[x][i]=p4(c[x][i],c[x][(i+3)&3],
			c[x][(i+2)&3],c[x][(i+1)&3]);
		g[x]=min(min(t[x][0],t[x][1]),min(t[x][2],t[x][3]));
		for(int i=0;i<4;++i)
			cc[x]+=(g[x]==t[x][i]);
		add(x);
	}
	for(int i=1;i<=n;++i)
	{
		for(int j=i+1;j<=n;++j)
		{
			del(i); del(j);
			for(int k=0;k<4;++k)
			{
				map<s4,int> tar;
				for(int p=0;p<4;++p)
					++tar[py(p4(c[i][p],c[i][(p+1)&3],
					c[j][(4-p+k)&3],c[j][(4-p+k+1)&3]))];
				ll ans=1;
				for(auto s:tar)
					ans*=ms[s.se][s.fi]*fac[s.se];
				aa+=ans;
			}
			add(i); add(j);
		}
	}
	printf("%lld\n",aa/3);
}