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# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    static readonly int[][] Index = { new[] { 1 ,0, 1, 0 }, new[] { 2, 1, 0, 3 },
                                      new[] { 3, 2, 3, 2 }, new[] { 0, 3, 2, 1 } };

    public void Solve()
    {
        int N = Reader.Int();
        var C = Reader.IntTable(N);
        var K = C.Select(c => Key(c)).ToArray();
        var M = new Dictionary<long, int>();
        for (int i = 0; i < N; i++) M[K[i]] = Mult(C[i]);
        var dic = new Dictionary<long, int>();
        ulong ans = 0;

        foreach (long k in K)
            dic[k] = dic.ContainsKey(k) ? dic[k] + 1 : 1;
        for (int a = 0; a < N - 1 && dic[K[a]]-- > 0; a++)
            for (int b = a + 1; b < N && dic[K[b]]-- > 0; dic[K[b]]++, b++)
                for (int r = 0; r < 4; r++, Rotate(C[b]))
                    ans += (ulong)Count(C[a], C[b], dic, M);

        Console.WriteLine(ans);
    }

    private long Count(int[] A, int[] B, Dictionary<long, int> dic, Dictionary<long, int> Mult)
    {
        var pattern = new Dictionary<long, int>();
        foreach (var i in Index)
        {
            long k = Key(new[] { A[i[0]], A[i[1]], B[i[2]], B[i[3]] });
            if (!dic.ContainsKey(k)) return 0;
            pattern[k] = pattern.ContainsKey(k) ? pattern[k] + 1 : 1;
        }
        long num = 1;
        foreach (long k in pattern.Keys)
            for (int i = 0; i < pattern[k]; i++)
                num *= (dic[k] - i) * Mult[k];
        return num;
    }

    void Rotate(int[] a) { int t = a[3]; Array.Copy(a, 0, a, 1, 3); a[0] = t; }

    long Key(int[] a)
    {
        long min = long.MaxValue;
        for (int r = 0; r < 4; r++, Rotate(a))
            min = Math.Min(min, a[0] * (long)1e9 + a[1] * (long)1e6 + a[2] * 1000 + a[3]);
        return min;
    }

    int Mult(int[] a)
    {
        if (a[0] == a[1] && a[1] == a[2] && a[2] == a[3]) return 4;
        if (a[0] == a[2] && a[1] == a[3]) return 2;
        return 1;
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}