atcoder#ARC062C. [ARC062E] AtCoDeerくんと立方体づくり

[ARC062E] AtCoDeerくんと立方体づくり

Score : 900900 points

Problem Statement

AtCoDeer the deer has NN square tiles. The tiles are numbered 11 through NN, 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 10001000 colors, which are represented by the integers 00 between 999999. The top-left, top-right, bottom-right and bottom-left corner of the tile with the number ii are painted in color Ci,0,Ci,1,Ci,2C_{i,0}, C_{i,1}, C_{i,2} and Ci,3C_{i,3}, respectively, when seen in the direction of the number written on the tile (See Figure 11).

Figure 11: 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 9090^\circ 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 22: The four directions of a tile

Constraints

  • 6N4006 \leq N \leq 400
  • $0 \leq C_{i,j} \leq 999 (1 \leq i \leq N , 0 \leq j \leq 3)$

Input

The input is given from Standard Input in the following format:

NN

C1,0C_{1,0} C1,1C_{1,1} C1,2C_{1,2} C1,3C_{1,3}

C2,0C_{2,0} C2,1C_{2,1} C2,2C_{2,2} C2,3C_{2,3}

::

CN,0C_{N,0} CN,1C_{N,1} CN,2C_{N,2} CN,3C_{N,3}

Output

Print the number of the different cubes that can be constructed under the conditions.

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
1

The cube below can be constructed.

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
144
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
122880