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 (Clang 3.8.0) (judge verdict: Accepted)

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <deque>
#include <stack>
#include <iomanip>
#include <cmath>


using namespace std;


typedef long long ll;
typedef long double ld;


const ll MOD = 1000000007ll;


ll modSum(ll a, ll b) {
  return ((a % MOD) + (b % MOD)) % MOD;
}


ll modSubtract(ll a, ll b) {
  return (((a % MOD) - (b % MOD)) + MOD + MOD) % MOD;
}


ll modProd(ll a, ll b) {
  return ((a % MOD) * (b % MOD)) % MOD;
}


ll getPowMod(ll x, ll e) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    ll tmp = getPowMod(x, e/2);
    return modProd(tmp, tmp);
  } else {
    ll tmp = getPowMod(x, e-1);
    return modProd(tmp, x);
  }
}


ll getPow(ll x, ll e) {
  if (e == 0) return 1;
  if (e % 2 == 0) {
    ll tmp = getPow(x, e/2);
    return tmp * tmp;
  } else {
    ll tmp = getPow(x, e-1);
    return tmp * x;
  }
}


ll getInverse(ll x) {
  return getPowMod(x, MOD-2);
}


bool isEven(ll x) {
  ll tmp = ((x % 2) + 2) % 2;
  return tmp == 0;
}


ll getSumOfDigitsInBase(ll n, ll b) {
  ll ret = 0;
  while (n > 0) {
    ret += n % b;
    n /= b;
  }
  return ret;
}


vector<int> getKMP(string &s) {
  int len = (int)s.size();
  vector<int> ret (len, 0);
  for (int i = 1 ; i < len ; i++) {
    int at = ret[i-1];
    while (at > 0 && s[i] != s[at]) {
      at = ret[at-1];
    }
    if (s[i] == s[at]) {
      at++;
    }
    ret[i] = at;
  }
  return ret;
}


string getSubstring(string &s, int from, int to) {
  int l = to-from+1;
  if (l <= 0) {
    return "";
  }
  return s.substr(from, l);
}

ll gcd(ll a, ll b, ll & x, ll & y) {
    if (a == 0) {
        x = 0;
        y = 1;
        return b;
    }
    ll x1, y1;
    ll d = gcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return d;
}












const int MAXN = 401;


int N;
tuple<int, int, int, int> ar[MAXN];
map< tuple<int, int, int, int>, int> freq;
set< pair< int, tuple<int, int, int, int> > > seen;



void input() {
  cin >> N;
  vector<int> tmp(4);
  for (int i = 1 ; i <= N ; i++) {
    for (int j = 0 ; j < 4 ; j++) {
      cin >> tmp[j];
    }
    bool inserted = false;
    for (int j = 0 ; j < 4 ; j++) {
      rotate(tmp.begin(), tmp.begin()+1, tmp.end());
      if (freq.find({tmp[0], tmp[1], tmp[2], tmp[3]}) != freq.end()) {
        inserted = true;
        freq[{tmp[0], tmp[1], tmp[2], tmp[3]}]++;
        break;
      }
    }
    if (!inserted) {
      freq[{tmp[0], tmp[1], tmp[2], tmp[3]}]++;
    }
    ar[i] = {tmp[0], tmp[1], tmp[2], tmp[3]};
  }
}


void preprocess() {
}


void solve() {
  ll ans = 0;
  //for (auto p : freq) {
  //  cerr << "(" << get<0>(p.first) << ", " << get<1>(p.first) << ", " << get<2>(p.first) << ", " << get<3>(p.first) << ") - " << p.second << "\n";
 // }
  vector< vector<int> > tmp(4, vector<int>(4));
  for (int i = 1 ; i <= N ; i++) {
    auto t1 = ar[i];
    freq[t1]--;
    for (int j = i+1 ; j <= N ; j++) {
      auto t2 = ar[j];
      if (seen.find({i, t2}) != seen.end()) {
        continue;
      }
      //cerr << "I = " << i << ", J = " << j << ", Ans = " << ans << "\n";
      vector<int> tmp1 = {get<0>(t1), get<1>(t1), get<2>(t1), get<3>(t1)};
      vector<int> tmp2 = {get<0>(t2), get<1>(t2), get<2>(t2), get<3>(t2)};
      reverse(tmp2.begin(), tmp2.end());
      ll origMul = freq[t2];
      freq[t2]--;

      for (int o = 0 ; o <= 3 ; o++) {
        ll mul = origMul;
        for (int s = 0 ; s <= 3 ; s++) {
          tmp[s][3] = tmp1[(s+0) % 4]; 
          tmp[s][2] = tmp1[(s+1) % 4]; 
          tmp[s][1] = tmp2[(s+o+1) % 4]; 
          tmp[s][0] = tmp2[(s+o+0) % 4]; 
          //for (auto x : tmp[s]) {
          //  cerr << x << ", "; 
         // }
          //cerr << "\n";
          ll add = 0;
          for (int r = 0 ; r < 4 ; r++) {
            rotate(tmp[s].begin(), tmp[s].begin()+1, tmp[s].end());
            auto it = freq.find({tmp[s][0], tmp[s][1], tmp[s][2], tmp[s][3]});
            if (it != freq.end()) {
              add += it->second;
            }
          }
          for (int r = 0 ; r < 4 ; r++) {
            rotate(tmp[s].begin(), tmp[s].begin()+1, tmp[s].end());
            auto it = freq.find({tmp[s][0], tmp[s][1], tmp[s][2], tmp[s][3]});
            if (it != freq.end()) {
              freq[it->first]--;
              break;
            }
          }
          //cerr << "Add = " << add << "\n";
          mul *= add;
        }
       // tuple<int, int, int, int> tup0 = {tmp[0][0], tmp[0][1], tmp[0][2], tmp[0][3]};
       // tuple<int, int, int, int> tup1 = {tmp[1][0], tmp[1][1], tmp[1][2], tmp[1][3]};
       // tuple<int, int, int, int> tup2 = {tmp[2][0], tmp[2][1], tmp[2][2], tmp[2][3]};
       // tuple<int, int, int, int> tup3 = {tmp[3][0], tmp[3][1], tmp[3][2], tmp[3][3]};
       // if (seen.find({tup0, tup2}) == seen.end() && seen.find({tup1, tup3}) == seen.end()) {
       //   if (mul > 0) {
       //     cerr << "O = " << o << "\n";
       //   }
          ans += mul;
        //}
        for (int s = 0 ; s <= 3 ; s++) {
          auto it = freq.find({tmp[s][0], tmp[s][1], tmp[s][2], tmp[s][3]});
          if (it != freq.end()) {
            freq[it->first]++;
          }
        }
      }
      freq[t2]++;
      seen.insert({i, t2});
    }
  }
  cout << ans;
}


int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);

  int T;
//  cin >> T;
  T = 1;

  for (int i = 0 ; i < T ; i++) {
    input();
    preprocess();
    solve();
  }

  return 0;
}