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;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}

vint rig(vint v){
    vector<vint>r(4);
    rep(i,4){
        r[i]=v;
        rotate(r[i].begin(),r[i].begin()+i,r[i].end());
    }
    sort(all(r));
    return r[0];
}

int en(vint v){
    int r=0;
    rep(i,v.size())r=r*1000+v[i];
    return r;
}

int N;
vector<int>C[400];
signed main(){
    cin>>N;
    map<int,int>cnt;
    map<int,int>way;
    rep(i,N){
        C[i].resize(4);
        rep(j,4)cin>>C[i][j];
        C[i]=rig(C[i]);
        cnt[en(C[i])]++;

        vint v=C[i];
        set<vint>s;
        rep(k,4){
            s.insert(v);
            rotate(v.begin(),v.begin()+1,v.end());
        }
        way[en(C[i])]=4/s.size();
    }

    int ans=0;
    rep(i,N){
        cnt[en(C[i])]--;
        vint X=C[i];
        reps(j,i+1,N){
            vint Y=C[j];
            cnt[en(C[j])]--;
            reverse(all(Y));

            rep(ii,4){

                vint wanna(4);
                rep(k,4){
                    vint v(4);
                    v[0]=X[(k+1)%4];
                    v[1]=X[k];
                    v[2]=Y[k];
                    v[3]=Y[(k+1)%4];
                    v=rig(v);
                    wanna[k]=en(v);
                }
                sort(all(wanna));
                vpint want;
                rep(k,4){
                    if(want.size()==0||want.back().fi!=wanna[k]){
                        want.push_back(pint(wanna[k],1));
                    }
                    else{
                        want[want.size()-1].se++;
                    }
                }

                int tmp=1;
                rep(k,want.size()){
                    int e=want[k].fi;
                    int f=want[k].se;
                    if(cnt.find(e)==cnt.end()){
                        tmp=0;
                        continue;
                    }
                    int hoge=cnt[e];
                    int piyo=way[e];
                    rep(l,f)tmp*=(hoge-l)*piyo;
                }
                ans+=tmp;
                rotate(Y.begin(),Y.begin()+1,Y.end());
            }
            cnt[en(C[j])]++;
        }
    }
    cout<<ans<<endl;
    return 0;
}