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: Memory Limit Exceeded)

#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<array>
#include<vector>
using namespace std;
long conv(int a,int b,int c,int d){return 1000000000L*a+1000000L*b+1000L*c+d;}
long rotate(long a){return a%1000000000L*1000+a/1000000000L;}
bool same(long a,long b)
{
	if(a==b)return true;
	a=rotate(a);
	if(a==b)return true;
	a=rotate(a);
	if(a==b)return true;
	a=rotate(a);
	if(a==b)return true;
	return false;
}
int N;
map<long,array<array<long,15>,4> >M;
map<long,map<int,int> >Q;
int a[400],b[400],c[400],d[400];
main()
{
	cin>>N;
	for(int i=0;i<N;i++)
	{
		cin>>a[i]>>b[i]>>c[i]>>d[i];
		long C=conv(a[i],b[i],c[i],d[i]);
		map<long,int>now;
		for(int r=0;r<4;r++)
		{
			now[C]++;
			C=rotate(C);
		}
		for(map<long,int>::iterator it=now.begin();it!=now.end();it++)
		{
			Q[it->first][i]=it->second;
		}
	}
	for(map<long,map<int,int> >::iterator it=Q.begin();it!=Q.end();it++)
	{
		array<array<long,15>,4>&A=M[it->first];
		long sum=0;
		int ct[5]={};
		for(map<int,int>::iterator jt=it->second.begin();jt!=it->second.end();jt++)
		{
			sum+=jt->second;
			ct[jt->second]++;
		}
		for(int j=0;j<5;j++)for(int k=0;k<=j;k++)
		{
			if(j&&ct[j]==0||k&&ct[k]==0)continue;
			ct[j]--;
			ct[k]--;
			A[0][j*(j+1)/2+k]=sum-j-k;
			A[1][j*(j+1)/2+k]=(sum-j-k)*(sum-j-k-1)-ct[2]*2-ct[3]*6-ct[4]*12;
			A[2][j*(j+1)/2+k]=(sum-j-k)*(sum-j-k-1)*(sum-j-k-2)
				-(sum-j-k-2)*ct[2]*6
				-(sum-j-k-3)*ct[3]*3*6-ct[3]*6
				-(sum-j-k-4)*ct[4]*6*6-ct[4]*24;
			A[3][j*(j+1)/2+k]=(sum-j-k)*(sum-j-k-1)*(sum-j-k-2)*(sum-j-k-3)
				-(sum-j-k-2)*(sum-j-k-2-1)/2*ct[2]*24
				-(sum-j-k-3)*(sum-j-k-3-1)/2*ct[3]*3*24-(sum-j-k-3)*ct[3]*24
				-(sum-j-k-4)*(sum-j-k-4-1)/2*ct[4]*6*24-(sum-j-k-4)*ct[4]*4*24-ct[4]*24
				+ct[2]*(ct[2]-1)/2*24
				+ct[2]*ct[3]*3*24
				+ct[2]*ct[4]*6*24
				+ct[3]*3*(ct[3]-1)*3/2*24
				+ct[3]*3*ct[4]*6*24
				+ct[4]*6*(ct[4]-1)*6/2*24;
			ct[j]++;
			ct[k]++;
		}
	}
	long ans=0;
	for(int i=0;i<N;i++)
	{
		for(int j=i+1;j<N;j++)
		{
			for(int ccc=0;ccc<4;ccc++)
			{
				int tmp=a[j];
				a[j]=b[j];
				b[j]=c[j];
				c[j]=d[j];
				d[j]=tmp;
				long t[4]={
					conv(b[i],a[i],b[j],a[j]),
					conv(a[i],d[i],c[j],b[j]),
					conv(d[i],c[i],d[j],c[j]),
					conv(c[i],b[i],a[j],d[j]),
				};
				int cc[4]={1,1,1,1};
				for(int I=0;I<4;I++)for(int J=I+1;J<4;J++)
				{
					if(cc[J]==0)continue;
					if(same(t[I],t[J]))cc[I]+=cc[J],cc[J]=0;
				}
				long now=1;
				for(int k=0;k<4;k++)
				{
					if(cc[k]==0)continue;
					int a=Q[t[k]][i];
					int b=Q[t[k]][j];
					if(a<b)a^=b^=a^=b;
					now*=M[t[k]][cc[k]-1][a*(a+1)/2+b];
				}
				ans+=now;
			}
		}
	}
	cout<<ans/3<<endl;
}