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<bits/stdc++.h>
using namespace std;
#define LL long long

const int mo=1e9+7;

int n,t1[5],t2[5],Ans;
LL tmp[4],s[410];
map<LL,int> S;

int power(int x,int k)
{
	int res=1;
	while (k)
	{
		if (k&1) res=(LL)res*x%mo;
		x=(LL)x*x%mo;
		k>>=1;
	}
	return res;
}

LL gethash(int a,int b,int c,int d)
{
	LL A=a*1000000000ll+b*1000000ll+c*1000ll+d;
	LL B=b*1000000000ll+c*1000000ll+d*1000ll+a;
	LL C=c*1000000000ll+d*1000000ll+a*1000ll+b;
	LL D=d*1000000000ll+a*1000000ll+b*1000ll+c;
	return min(min(A,B),min(C,D));
}

void getcolor(LL hs,int &a,int &b,int &c,int &d)
{
	d=hs%1000; hs/=1000;
	c=hs%1000; hs/=1000;
	b=hs%1000; hs/=1000;
	a=hs%1000;
}

int main()
{
#ifdef h10
	freopen("E.in","r",stdin);
	freopen("E.out","w",stdout);
#endif
	int i,j,x,y,a,b,c,d;
	scanf("%d",&n);
	for (i=1;i<=n;i++)
	{
		scanf("%d%d%d%d",&a,&b,&c,&d);
		s[i]=gethash(a,b,c,d);
	}
	sort(s+1,s+n+1);
	for (i=1;i<=n;i++) S[s[i]]++;
	for (i=1;i<=n;i++)
	for (j=i+1;j<=n;j++)
	{
		getcolor(s[i],t1[0],t1[1],t1[2],t1[3]);
		getcolor(s[j],t2[0],t2[1],t2[2],t2[3]);
		S[s[i]]--; S[s[j]]--;
		for (x=0;x<4;x++)
		{
			tmp[0]=gethash(t2[1],t2[0],t1[1],t1[0]);
			tmp[1]=gethash(t2[0],t2[3],t1[2],t1[1]);
			tmp[2]=gethash(t2[3],t2[2],t1[3],t1[2]);
			tmp[3]=gethash(t2[2],t2[1],t1[0],t1[3]);
			sort(tmp,tmp+4);
			int way,Sum=1;
			for (y=0;y<4;y++)
			{
				if (!y||tmp[y]!=tmp[y-1])
					way=S[tmp[y]];
				else
					way--;
				Sum=(LL)Sum*way%mo;
				getcolor(tmp[y],a,b,c,d);
				if (a==c&&b==d)
				{
					Sum+=Sum%=mo;
					if (a==b) Sum+=Sum%=mo;
				}
			}
			(Ans+=Sum)%=mo;
			t1[4]=t1[0];
			for (y=0;y<4;y++) t1[y]=t1[y+1];
		}
		S[s[i]]++; S[s[j]]++;
	}
	Ans=(LL)Ans*power(3,mo-2)%mo;
	printf("%d\n",Ans);
}