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.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static System.Math;
using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute;
using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions;

public static class P
{
    public static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        Tile[] tiles = Enumerable.Range(0, n).Select(x =>
        {
            var c = Console.ReadLine().Split().Select(int.Parse).ToArray();
            return new Tile(x, c[0], c[1], c[2], c[3]);
        }).ToArray();
        Dictionary<long, int> tileCounts = new Dictionary<long, int>();
        foreach (var tile in tiles)
        {
            if (!tileCounts.ContainsKey(tile.NormalSeed)) tileCounts.Add(tile.NormalSeed, 0);
            tileCounts[tile.NormalSeed]++;   
        }
        long res = 0;
        foreach (var topTile in tiles)
        {
            var topSeed = topTile.NormalSeed;
            foreach (var bottomTile in tiles.Where(x => x.Num != topTile.Num))
            {
                var bottomSeed = bottomTile.NormalSeed;
                foreach (var rotated in bottomTile.Rotates)
                {
                    //LU  RU
                    //  up
                    //LD  RD

                    //RU  LU
                    //  dn
                    //RD  LD
                    var tile1 = new Tile(0, topTile.RU, topTile.LU, rotated.RU, rotated.LU);
                    var tile2 = new Tile(0, topTile.RD, topTile.RU, rotated.LU, rotated.LD);
                    var tile3 = new Tile(0, topTile.LD, topTile.RD, rotated.LD, rotated.RD);
                    var tile4 = new Tile(0, topTile.LU, topTile.LD, rotated.RD, rotated.RU);
                    var seed1 = tile1.NormalSeed;
                    var seed2 = tile2.NormalSeed;
                    var seed3 = tile3.NormalSeed;
                    var seed4 = tile4.NormalSeed;
                    var count1 = tileCounts.Get(seed1) - (topSeed == seed1 ? 1 : 0) - (bottomSeed == seed1 ? 1 : 0);
                    var count2 = tileCounts.Get(seed2) - (topSeed == seed2 ? 1 : 0) - (bottomSeed == seed2 ? 1 : 0) - (seed1 == seed2 ? 1 : 0);
                    var count3 = tileCounts.Get(seed3) - (topSeed == seed3 ? 1 : 0) - (bottomSeed == seed3 ? 1 : 0) - (seed1 == seed3 ? 1 : 0) - (seed2 == seed3 ? 1 : 0);
                    var count4 = tileCounts.Get(seed4) - (topSeed == seed4 ? 1 : 0) - (bottomSeed == seed4 ? 1 : 0) - (seed1 == seed4 ? 1 : 0) - (seed2 == seed4 ? 1 : 0) - (seed3 == seed4 ? 1 : 0);
                    var perm = (long)count1 * tile1.Order * count2 * tile2.Order * count3 * tile3.Order * count4 * tile4.Order;
                    res += perm;
                }
            }
        }
        res /= 6;
        Console.WriteLine(res);
    }
    static TVal Get<TKey, TVal>(this Dictionary<TKey, TVal> dict, TKey key) => dict.ContainsKey(key) ? dict[key] : default(TVal);
}

struct Tile
{
    //   0
    // 　↑
    //3←　→1
    //　 ↓
    //   2
    public int Num;
    public int Dir;
    //L←→R
    public int RU;
    public int RD;
    public int LU;
    public int LD;
    public long NormalSeed => Rotates.Min(x => x.ColorSeed);
    public long ColorSeed => GenColorSeed(LU, RU, RD, LD);
    public int Order => (RU == LD && LU == RD ? (RU == LU ? 4 : 2) : 1);
    public IEnumerable<Tile> Rotates
    {
        get
        {
            yield return this;
            var tile = this.Rotate();
            yield return tile;
            tile = tile.Rotate();
            yield return tile;
            tile = tile.Rotate();
            yield return tile;
        }
    }
    public Tile(int num, int lu, int ru, int rd, int ld)
    {
        Num = num;
        Dir = 0;
        RU = ru;
        RD = rd;
        LU = lu;
        LD = ld;
    }
    public Tile Rotate() => new Tile()
    {
        Num = Num,
        Dir = (Dir + 1) & 3,
        RU = RD,
        RD = LD,
        LD = LU,
        LU = RU
    };
    public static long GenColorSeed(int lu, int ru, int rd, int ld) => ((((long)lu * 1000) + ru) * 1000 + rd) * 1000 + ld;
}