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: Time Limit Exceeded)

// includes {{{
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<tuple>
#include<cmath>
#include<random>
#include<cassert>
// #include<deque>
// #include<multiset>
// #include<bitset>
// #include<cstring>
// #include<bits/stdc++.h>
// }}}
using namespace std;
using ll = long long;

int n;
using Panel = array<int, 4>;

Panel rotate(Panel a) {
  tie(a[0], a[1], a[2], a[3]) = make_tuple(a[1], a[2], a[3], a[0]);
  return a;
}

Panel normalize(Panel a) {
  Panel p[4];
  for(int i = 0; i < 4; i++) {
    p[i] = a;
    a = rotate(a);
  }
  return *min_element(p, p + 4);
}

const int N = 400;

int rotcount(Panel a) {
  auto b = a;
  int res = 0;
  for(int i = 0; i < 4; i++) {
    b = rotate(b);
    if(a == b) res++;
  }
  return res;
}

ll fact[10];
ll comb[N + 1][N + 1];

Panel a[N];

int main() {
  ios::sync_with_stdio(false), cin.tie(0);
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> a[i][0] >> a[i][1] >> a[i][2] >> a[i][3];
    a[i] = normalize(a[i]);
  }

  comb[0][0] = 1;
  for(int i = 1; i <= n; i++) for(int j = 0; j <= n; j++) {
    // Pascal's identity
    if(j == 0) {
      comb[i][j] = 1;
      continue;
    }
    comb[i][j] = comb[i-1][j-1] + comb[i-1][j];
  }

  fact[0] = 1;
  for(int i = 1; i <= 9; i++) fact[i] = fact[i-1] * i;

  ll ans = 0;
  for(int i = 0; i < n; i++) for(int j = i + 1; j < n; j++) {
    for(int rot = 0; rot < 4; rot++) {
      a[i] = rotate(a[i]);
      map<Panel, int> target;
      map<Panel, int> cnt;
      for(int k = 0; k < 4; k++) {
        Panel f{a[i][(k + 1) % 4], a[i][k + 0], a[j][(4 - k) % 4], a[j][(3 - k) % 4]};
        f = normalize(f);
        target[f]++;
        cnt[f] = 0;
      }
      for(int l = 0; l < n; l++) {
        if(l == i) continue;
        if(l == j) continue;
        if(target.count(a[l])) cnt[a[l]]++;
      }

      int ng = 0;
      ll res = 1;
      for(auto &p : target) {
        Panel pan;
        int tar;
        tie(pan, tar) = p;
        if(cnt[pan] < tar) {ng = 1; break;}
        res *= comb[cnt[pan]][tar] * pow((ll) rotcount(pan), (ll) tar) * fact[tar];
      }
      if(ng) continue;
      ans += res;
    }
  }
  assert(ans % 3 == 0);
  cout << ans/3 << endl;
  return 0;
}