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;
using uint = unsigned int;
using lint = long long int;
using ulint = unsigned long long int;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;
template<class T, class U> void assign(V<T>& v, int n, const U& a) { v.assign(n, a); }
template<class T, class... Args> void assign(V<T>& v, int n, const Args&... args) { v.resize(n); for (auto&& e : v) assign(e, args...); }


lint key(const V<>& v) {
  lint res = 0;
  for (int i = 0; i < 4; ++i) {
    lint curr = 0;
    for (int j = 0; j < 4; ++j) curr += static_cast<lint>(v[i + j & 3]) << 10 * j;
    res = max(res, curr);
  }
  return res;
}

int coeff(lint key) {
  V<> v(4);
  for (int i = 0; i < 4; ++i) {
    v[i] = key & 1023;
    key >>= 10;
  }
  if (v[0] == v[2] and v[1] == v[3]) {
    if (v[0] == v[1]) return 4;
    return 2;
  }
  return 1;
}

int main() {
  cin.tie(nullptr); ios_base::sync_with_stdio(false);
  int n; cin >> n;
  VV<> c; assign(c, n, 4, 0);
  for (int i = 0; i < n; ++i) for (int j = 0; j < 4; ++j) cin >> c[i][j];
  map<lint, lint> mp;
  for (int i = 0; i < n; ++i) ++mp[key(c[i])];
  lint res = 0;
  for (int d = 0; d < n; ++d) for (int u = d + 1; u < n; ++u) {
    --mp[key(c[d])], --mp[key(c[u])];
    for (int dir = 0; dir < 4; ++dir) {
      V<lint> keys(4);
      for (int i = 0; i < 4; ++i) keys[i] = key({c[u][dir - i & 3], c[u][dir - i - 1 & 3], c[d][i + 1 & 3], c[d][i]});
      sort(begin(keys), end(keys));
      int bit = 0;
      for (int i = 0; i < 3; ++i) if (keys[i] == keys[i + 1]) bit |= 1 << i;
      auto f = [&](const V<>& v) -> lint {
        lint res = 1;
        for (int i = 0; i < 4; ++i) res *= coeff(keys[i]) * (mp[keys[i]] - v[i]);
        return res;
      };
      switch (bit) {
        case 0b000: res += f({0, 0, 0, 0}); break;
        case 0b001: res += f({1, 0, 0, 0}); break;
        case 0b010: res += f({0, 0, 1, 0}); break;
        case 0b011: res += f({0, 1, 2, 0}); break;
        case 0b100: res += f({0, 0, 0, 1}); break;
        case 0b101: res += f({0, 1, 0, 1}); break;
        case 0b110: res += f({0, 0, 1, 2}); break;
        case 0b111: res += f({0, 1, 2, 3}); break;
      }
    }
    ++mp[key(c[d])], ++mp[key(c[u])];
  }
  cout << res / 3 << '\n';
}