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>
#define FOR(i,a,b) for(int i = (a);i<=(b);i++)
#define ROF(i,a,b) for(int i = (a);i>=(b);i--)
#define MST(a,x) memset(a,x,sizeof(a))
#define ll long long
#define PB push_back
#define PH push
#define MP make_pair
#define FT first
#define SD second
#define N 405
#define M 51
#define INF 500
#define MOD 924844033
#define MOD2 1000000009
#define eps 1e-7
using namespace std;
inline int Max(int a,int b)
{
    return (((b-a)>>(32-1))&(a^b))^b;
}
inline int Min(int a,int b)
{
    return (((b-a)>>(32-1))&(a^b))^a;
}

ll a[N];
ll x[N][4];
ll c[N][10];
ll dp[N][10];
int mp[4][4] = {{1,0,3,2},{0,3,2,1},{3,2,1,0},{2,1,0,3}};
map<ll,int>m;
ll get(ll x,ll y,ll z,ll h)
{
    ll ret = x;
    ret = (ret<<10)+y;
    ret = (ret<<10)+z;
    ret = (ret<<10)+h;
    return ret;
}
ll pack(ll x,ll y,ll z,ll h)
{
    return min(min(get(x,y,z,h),get(y,z,h,x)),min(get(z,h,x,y),get(h,x,y,z)));
}
ll fac(ll x)
{
    ll ret = 1;
    FOR(i,1,x)ret*=i;
    return ret;
}
ll rot(ll key)
{
    int k[4];
    FOR(i,0,3)
    {
        k[i] = key&((1<<10)-1);
        key>>=10;
    }
    if(k[0] == k[1]&&k[1] == k[2]&&k[2] == k[3])return 4;
    if(k[0] == k[2]&&k[1] == k[3])return 2;
    return 1;
}
int main()
{
    //cin.tie(0);
    //ios::sync_with_stdio(false);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        c[1][0] = c[1][1] = 1;
        FOR(i,2,n)
        {
            c[i][0] = 1;
            FOR(j,1,min(4,i-1))c[i][j] = c[i-1][j]+c[i-1][j-1];
            c[i][i] = 1;
        }
        FOR(i,1,n)
        {
            FOR(j,0,3)scanf("%d",&x[i][j]);
            a[i] = pack(x[i][0],x[i][1],x[i][2],x[i][3]);
        }
        ll ans = 0;
        m.clear();
        FOR(i,1,n)
        {
            FOR(ii,1,i-1)
            {
                FOR(j,0,3)
                {
                    ll y[4];
                    y[0] = pack(x[ii][(j+1)%4],x[ii][(j+0)%4],x[i][1],x[i][0]);
                    y[1] = pack(x[ii][(j+0)%4],x[ii][(j+3)%4],x[i][2],x[i][1]);
                    y[2] = pack(x[ii][(j+3)%4],x[ii][(j+2)%4],x[i][3],x[i][2]);
                    y[3] = pack(x[ii][(j+2)%4],x[ii][(j+1)%4],x[i][0],x[i][3]);

                    sort(y,y+4);
                    ll ret = 1;
                    for(int p = 0;p<4;)
                    {
                        int cnt = 1;
                        for(int r = p+1;r<4&&y[r] == y[p];r++)cnt++;
                        int num = m.count(y[p])?m[y[p]]:0;
                        if(y[p] == a[ii])num--;
                        if(num<cnt)
                        {
                            ret = 0;
                            break;
                        }
                        ret*=c[num][cnt]*pow(rot(y[p]),cnt)*fac(cnt);
                        p+=cnt;
                    }
                    ans+=ret;
                }
            }
            if(m.count(a[i]))m[a[i]]++;
            else m[a[i]] = 1;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}