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 (GCC 5.4.1) (judge verdict: Accepted)

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<stdio.h>

long long calcce(long long pt[]){
  if(pt[0]==pt[1]&&pt[1]==pt[2]&&pt[2]==pt[3]){return 4;}
  if(pt[0]==pt[2]&&pt[1]==pt[3]){return 2;}
  return 1;
}

long long cha[1024][4];
long long ispt(long long tg,long long hash){
  if(cha[tg][0]==hash){return 1;}
  if(cha[tg][1]==hash){return 1;}
  if(cha[tg][2]==hash){return 1;}
  if(cha[tg][3]==hash){return 1;}
  return 0;
}

long long calc(long long cnt[]){
  long long i,j,k,l,res=0,dt;
  long long pj,pk,pl,lm=0;
  for(i=8;i<16;i++){lm+=cnt[i];}
  //printf("%lld\n",lm);
  //for(i=0;i<16;i++){printf("[%lld]",cnt[i]);}printf("\n");
  for(i=1;i<16;i+=2){//if((i&1)==0){continue;}
    if(cnt[i]==0){continue;}
    if(i&8ll){lm--;}
    dt=cnt[i];cnt[i]--;
    pj=dt;
    for(j=0;j<16;j++){if((j&2)==0){continue;}
      if(cnt[j]==0){continue;}
      if(j&8ll){lm--;}
      dt*=cnt[j];cnt[j]--;
      pk=dt;
      for(k=0;k<16;k++){if((k&4)==0){continue;}
        if(cnt[k]==0){continue;}
        if(k&8ll){lm--;}
        dt*=cnt[k];cnt[k]--;
        res+=dt*lm;
        //for(l=8;l<16;l++){//if((l&8)==0){continue;}
        //  if(cnt[l]==0){continue;}
        //  dt*=cnt[l];cnt[l]--;
        //  res+=dt;
        //  cnt[l]++;
        //  dt=pl;
        //}
        if((k&8ll)!=0){lm++;}
        cnt[k]++;
        dt=pk;
      }
      if((j&8ll)!=0){lm++;}
      cnt[j]++;
      dt=pj;
    }
    if((i&8ll)!=0){lm++;}
    cnt[i]++;
  }
  return res;
}

int main(){
  srand(0);
  long long n,i,j,k,l,m,f,ce,res=0,tf,mem;
  long long c[512][4];
  long long jt[4],kt[4];
  long long md[4][4];
  long long cnt[16]={0};
  long long nha,mha[4];
  scanf("%lld",&n);
  //n=400;
  for(i=0;i<n;i++){
    nha=0;
    for(j=0;j<4;j++){
      scanf("%lld",&c[i][j]);
      //c[i][j]=rand()%4;
      nha|=(c[i][j]<<(10*j));
    }
    for(j=0;j<4;j++){
      cha[i][j]=nha;
      mem=(nha&((1ll<<10)-1));
      //printf("[%lld]<%lld>",cha[i][j],mem);
      nha>>=10;
      nha|=(mem<<30);
    }//printf("\n");
  }
  for(i=0;i<n;i++){
    for(j=0;j<4;j++){jt[j]=c[i][j];}
    for(j=i+1;j<n;j++){
      for(k=0;k<4;k++){
        for(l=0;l<4;l++){
          kt[l]=c[j][(4+k-l)&3ll];
        }
        ce=1;
        for(l=0;l<4;l++){
          md[l][0]=jt[l];
          md[l][1]=jt[(l+3)&3ll];
          md[l][2]=kt[(l+3)&3ll];
          md[l][3]=kt[l];
          mha[l]=(md[l][0]|(md[l][1]<<10)|(md[l][2]<<20)|(md[l][3]<<30));
          //printf("<%lld %lld %lld %lld : %lld>\n",md[l][0],md[l][1],md[l][2],md[l][3],mha[l]);
          ce*=calcce(md[l]);
        }
        for(l=0;l<16;l++){cnt[l]=0;}
        tf=0;
        for(l=i+1;l<n;l++){
          if(l==j){continue;}
          f=0;
          for(m=0;m<4;m++){
            if(ispt(l,mha[m])==1){f|=(1ll<<m);}
          }
          //printf("%lld\n",f);
          tf|=f;
          cnt[f]++;
        }
        if(tf!=15){continue;}
        res+=ce*calc(cnt);
      }
    }
  }
  printf("%lld\n",res);
  return 0;
}