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>
//#pragma comment(linker,"/STACK:1024000000,1024000000")
//251103FD
//4008802802
//4008160009
using namespace std;
#define PB pop_back
#define PF pop_front
#define np next_permutation
#define eb emplace_back
#define pb push_back
#define bf push_front
#define mp make_pair
#define s second
#define f first
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define Foreach(it, a) for(__typeof((a).rbegin()) it=(a).rbegin(); it!=(a).rend(); it++)
#define forn(i, a) for(register int i=0; i<a; i++)
#define Forn(i, a) for(register int i=1; i<=a; i++)
#define memINF 127
#define all(x) x.begin(), x.end()
#define FAST ios::sync_with_stdio(false)
#define lid id<<1|1
#define rid (id<<1)+2
#define mid l+r>>1
#define lson lid, l, mid
#define rson rid, mid, r

typedef long long ll;
typedef pair<int,int> pi;
typedef pair<pi,int> pii;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef unsigned int uint;

inline int in() 
{
    int x=0, c;
    while((uint)((c=getchar())-48)>=10) 
		if(c=='-') 
			return -in();
    do 
	{ x=(x<<3)+(x<<1)+(c^48); }
	while((uint)((c=getchar())^48)<10);
    return x;
}
inline int lcm(int a, int b){return a*b/__gcd(a, b);}

const int MAX=405;
const int mod=1e7+9;
const int MAX_H=1e5+5;

struct Hash
{
    int nex, w, s;
    ll val;
    Hash(int _n=0, int _w=0, int _s=0, ll _v=0):nex(_n), w(_w), s(_s), val(_v){}
}h[MAX];

int tot, N, t, C[MAX][5], head[MAX_H*10];
ll res, tt;

inline void Insert(ll H, int t)
{
    int fr=H/mod, id=head[fr];
    while(id&&h[id].val!=H)
        id=h[id].nex;
    if(id)
    {
        h[id].w+=t;
        return;
    }
    id=++tot, h[id].nex=head[fr], h[id].val=H, head[fr]=id, h[id].w=t;
    h[id].s=H/(ll)1e6==H%(ll)1e6?(H/(ll)1e9==H%(ll)1e3?4:2):1;
}

inline int Find(ll H)
{
    int fr=H/mod, id=head[fr];
    while(id&&h[id].val!=H)
        id=h[id].nex;
    return h[id].w*h[id].s;
}

inline ll Get_Hash(int *t, int turn)
{
    ll ret=0;
    forn(i, 4)
        ret=ret*1000+t[(i+turn)&3];
    return ret;
}

inline void Add(ll H, int t)
{
    ll tmp[4];
    forn(i, 4)
        tmp[i]=H, H=H/1000+H%1000*1000000000;
    sort(tmp, tmp+4);
    int ptr=unique(tmp, tmp+4)-tmp;
    forn(i, ptr)
        Insert(tmp[i], t);
}

inline void Add(int *T, int t)
{
    Add(Get_Hash(T, 0), t);
}

int main()
{
    N=in();
    forn(i, N)
    {
        forn(j, 4)
            C[i][j]=in();
        Add(C[i], 1);
    }
    forn(i, N)
    {
        Add(C[i], -1);
        for(int j=i+1; j<N; ++j)
        {
            Add(C[j], -1);
            reverse(C[j], C[j]+4);
            
            forn(k, 4)
            {
                tt=1;
                for(t=0; t<4; ++t)
                {
                    ll h=((C[j][(k+t)&3]*1000LL+C[j][(k+t+1)&3])*1000+C[i][(t+1)&3])*(ll)1000+C[i][t];
                    tt*=Find(h);
                    if(!tt)
                        break;
					Add(h, -1);
                }
                while(t--)
                {
                    ll h=((C[j][(k+t)&3]*1000LL+C[j][(k+t+1)&3])*(ll)1000+C[i][(t+1)&3])*(ll)1000+C[i][t];
                    Add(h, 1);
                }
                res+=tt;
            }
            reverse(C[j], C[j]+4);
            Add(C[j], 1);
        }
    }
    printf("%lld\n", res);
    return 0;
}