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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

typedef vector<int> vi;

inline vi normarize(const vi &v)
{
    vector<vi> r(4);
    rep(i,4)
    {
        vi t(4);
        rep(j,4) t[j]=v[(i+j)%4];
        r[i]=t;
    }
    sort(all(r));
    return r[0];
}

inline ll DIM(const vi &v)
{
    if(v[0]==v[1] && v[1]==v[2] && v[2]==v[3]) return 4;
    if(v[0]==v[2] && v[1]==v[3]) return 2;
    return 1;
}

int main()
{
    ll P[401][5]={0};
    for(int i=1; i<=400; ++i)
    {
        P[i][1]=i;
        for(int j=2; j<=min(4,i); ++j) P[i][j]=P[i][j-1]*(i-j+1);
    }

    ll POW[5][5];
    rep(i,5)
    {
        POW[i][0]=1;
        for(int j=1; j<5; ++j) POW[i][j]=POW[i][j-1]*i;
    }

    int n;
    scanf(" %d", &n);

    vector<vi> c(n,vi(4));
    rep(i,n)rep(j,4) scanf(" %d", &c[i][j]);

    map<vi,int> ct;
    rep(i,n)
    {
        c[i]=normarize(c[i]);
        if(ct.find(c[i])==ct.end()) ct[c[i]]=1;
        else ++ct[c[i]];
    }

    ll ans=0;
    // 上面i, 下面j, 下面の向きを設定
    rep(i,n)rep(j,i)rep(k,4)
    {
        vi u(4);
        rep(x,4) u[x]=c[j][(k+x)%4];
        swap(u[1],u[3]);

        vector<vi> sides(4);
        rep(x,4)
        {
            vi t(4);
            t[0]=c[i][(x+1)%4];
            t[1]=c[i][x];
            t[2]=u[x];
            t[3]=u[(x+1)%4];
            sides[x]=normarize(t);
        }
        sort(all(sides));

        ll add=1;
        int idx=0, st=0;
        while(1)
        {
            while(idx<4 && sides[st]==sides[idx]) ++idx;
            vi key=sides[st];
            int value=idx-st;

            // 使ってないタイルのうち当てはめられる個数
            int num=ct[key];
            if(c[i]==key) --num;
            if(c[j]==key) --num;

            if(num<value)
            {
                add=0;
                break;
            }

            // 選び方、向き、位置
            add*=P[num][value]*POW[DIM(key)][value];

            if(idx==4) break;
            st=idx;
        }
        ans+=add;
    }

    // 向かい合う面の個数ぶん重複する
    cout << ans/3 << endl;
    return 0;
}