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

The cube below can be constructed.

Difficulty: competition

Starter code:

Solutions:
from collections import defaultdict
N, = list(map(int, input().split()))

def normal(xs):
    return tuple(min((xs[j:] + xs[:j] for j in range(1, 5))))

dd = defaultdict(int)
cc = dict()
norm = dict()
ss = []
for _ in range(N):
    xs = list(map(int, input().split()))
    cnd = [tuple(xs[j:] + xs[:j]) for j in range(1, 5)]
    x = min(cnd)
    for item in cnd:
        norm[item] = x
    dd[x] += 1
    cc[x] = (4 if x[0] == x[1] else 2)if x[0] == x[2] and x[1] == x[3] else 1
    ss.append(x)

def icr(x):
    dd[x] += 1

def dcr(x):
    dd[x] -= 1

def f(ff, gg):
    a,b,c,d=ff
    e,h,g,f=gg
    tl = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
    for cp in tl:
        if cp not in norm:
            return 0
    r = 1
    for cp in tl:
        cp = norm[cp]
        r *= dd[cp]*cc[cp]
        dcr(cp)
    for cp in tl:
        cp = norm[cp]
        icr(cp)
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dcr(ff)
    for j in range(i+1, N):
        sl = ss[j]
        dcr(sl)
        x, y, z, w = sl
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        icr(sl)
    icr(ff)
print((r//3))


from collections import defaultdict
N, = list(map(int, input().split()))

dd = defaultdict(int)
cc = dict()
norm = dict()
ss = []
for _ in range(N):
    xs = list(map(int, input().split()))
    cnd = [tuple(xs[j:] + xs[:j]) for j in range(1, 5)]
    x = min(cnd)
    for item in cnd:
        norm[item] = x
    dd[x] += 1
    cc[x] = (4 if x[0] == x[1] else 2)if x[0] == x[2] and x[1] == x[3] else 1
    ss.append(x)

def f(ff, gg):
    a,b,c,d=ff
    e,h,g,f=gg
    tl = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
    for i in range(4):
        if tl[i] not in norm:
            return 0
        tl[i] = norm[tl[i]]
    r = 1
    for cp in tl:
        r *= dd[cp]*cc[cp]
        dd[cp] -= 1
    for cp in tl:
        dd[cp] += 1
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dd[ff]-=1
    for j in range(i+1, N):
        sl = ss[j]
        x, y, z, w = sl
        dd[sl]-=1
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        dd[sl]+=1
    dd[ff]+=1
print((r//3))


from collections import deque
N = int(input())
C = []
M = {}
for i in range(N):
    *c, = list(map(int, input().split()))
    c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
    C.append(c)
    if c not in M:
        M[c] = deque([i])
    else:
        M[c].append(i)
def count(p, q, r, s):
    if p == q == r == s:
        return 4
    if p == r and q == s:
        return 2
    return 1
def solve(ci, cj, k):
    R = {}
    for l in range(4):
        #  [l]   [l-1]
        # [l+k] [l+k+1]
        c = ci[l], ci[l-1], cj[k-l], cj[k-l-1]
        c = tuple(min(c[j:] + c[:j] for j in range(1, 5)))
        if c not in M:
            return 0
        R[c] = R.get(c, 0) + 1
    res = 1
    for c in R:
        m = M[c]
        cnt = len(m)
        if c == cj:
            cnt -= 1
        if cnt < R[c]:
            return 0
        k = count(*c)
        for p in range(cnt-R[c]+1, cnt+1):
            res *= p * k
    return res

ans = 0
for i in range(N):
    ci = C[i]
    q = M[ci]; q.popleft()
    if not q:
        del M[ci]
    for j in range(i+1, N):
        cj = C[j]
        for k in range(4):
            ans += solve(ci, cj, k)
print(ans)


import sys
from collections import Counter
readline = sys.stdin.readline

def compress(L):
    L2 = list(set(L))
    L2.sort()
    C = {v : k for k, v in enumerate(L2, 1)}
    return L2, C

def order(a, b, c, d):
    if a == b == c == d:
        return 4
    if a == c and b == d:
        return 2
    return 1


limit = 5
calc = [[None]*limit for _ in range(limit*400)]
for i in range(limit*400):
    calc[i][1] = i
    for j in range(2, limit):
        calc[i][j] = calc[i][j-1]*(i-j+1)
pp = [[pow(i, j) for j in range(10)] for i in range(10)]

N = int(readline())
C = [tuple(map(int, readline().split())) for _ in range(N)]
D = Counter()



Rot = []
for i in range(N):
    a, b, c, d = C[i]
    Rot.append((a, b, c, d))
    Rot.append((d, a, b, c))
    Rot.append((c, d, a, b))
    Rot.append((b, c, d, a))
Lc, Cr = compress(Rot)
Lc = [None] + Lc
Cc = []

Od = Counter()
Base = Counter()
D = Counter()
for i in range(N):
    a, b, c, d = C[i]
    a, b, c, d = min((a, b, c, d), (b, c, d, a), (c, d, a, b), (d, a, b, c))
    od = order(a, b, c, d)
    r1 = Cr[(a, b, c, d)]
    r2 = Cr[(b, c, d, a)]
    r3 = Cr[(c, d, a, b)]
    r4 = Cr[(d, a, b, c)]
    Base[r1] = r1
    Base[r2] = r1
    Base[r3] = r1
    Base[r4] = r1
    Od[r1] = od
    Od[r2] = od
    Od[r3] = od
    Od[r4] = od
    Cc.append((r1, r2, r3, r4))
    D[r1] += 1


ans = 0
for i in range(N):
    D[Cc[i][0]] -= 1
    a, b, c, d = Lc[Cc[i][0]]
    for j in range(i+1, N):
        D[Cc[j][0]] -= 1
        for idx in range(4):
            e, f, g, h = Lc[Cc[j][idx]]
            E = Counter()
            r1 = (b, e, h, c)
            if r1 not in Cr:
                continue
            r1 = Base[Cr[r1]]
            r2 = (a, f, e, b)
            if r2 not in Cr:
                continue
            r2 = Base[Cr[r2]]
            r3 = (d, g, f, a)
            if r3 not in Cr:
                continue
            r3 = Base[Cr[r3]]
            r4 = (c, h, g, d)
            if r4 not in Cr:
                continue
            r4 = Base[Cr[r4]]
            
            E[r1] += 1
            E[r2] += 1
            E[r3] += 1
            E[r4] += 1            
            res = 1
            for k, n in list(E.items()):
                res *= calc[D[k]][n] * pp[Od[k]][n]
            ans += res
        
        D[Cc[j][0]] += 1

print(ans)


from collections import defaultdict
N, = list(map(int, input().split()))

def normal(xs):
    mnx = min(xs)
    xi = xs.index(mnx)
    if xs[(xi+3)%4] == mnx:
        if xs[(xi+2)%4] == mnx:
            xi = (xi+2)%4
        else:
            xi = (xi+3)%4
    if xs[(xi+1)%4] > xs[(xi+3)%4]:
        xi = (xi+2)%4
    return (xs[xi%4], xs[(xi+1)%4], xs[(xi+2)%4], xs[(xi+3)%4])
#x = (0,2,2,0)
#print(normal(x))

dd = defaultdict(int)
cc = defaultdict(int)
ss = []
for _ in range(N):
    a, b, c, d = list(map(int, input().split()))
    x = normal([a,b,c,d])
    n=1
    if x[0] == x[2] and x[1] == x[3]:
        n *= 2
        if x[0] == x[1]:
            n *= 2
    dd[x] += 1
    cc[x] = n
    ss.append(x)

def icr(x):
    dd[x] += 1

def dcr(x):
    dd[x] -= 1

def f(ff, gg):
    #print(dd)
    a,b,c,d=ff
    e,h,g,f=gg
    tl = list(map(normal, [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]))
    r = 1
    for cp in tl:
        if cp not in dd:
            r = 0
            break
        r *= dd[cp]*cc[cp]
        dcr(cp)
    for cp in tl:
        if cp not in dd:
            break
        icr(cp)
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dcr(ff)
    for j in range(i+1, N):
        sl = ss[j]
        dcr(sl)
        x, y, z, w = sl
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        icr(sl)
    icr(ff)
print((r//3))


from collections import defaultdict
N, = map(int, input().split())

dd = defaultdict(int)
cc = dict()
norm = dict()
ss = []
for _ in range(N):
    xs = list(map(int, input().split()))
    cnd = [tuple(xs[j:] + xs[:j]) for j in range(1, 5)]
    x = min(cnd)
    for item in cnd:
        norm[item] = x
    dd[x] += 1
    cc[x] = (4 if x[0] == x[1] else 2)if x[0] == x[2] and x[1] == x[3] else 1
    ss.append(x)

def f(ff, gg):
    a,b,c,d=ff
    e,h,g,f=gg
    tl = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
    for i in range(4):
        if tl[i] not in norm:
            return 0
        tl[i] = norm[tl[i]]
    r = 1
    for cp in tl:
        r *= dd[cp]*cc[cp]
        dd[cp] -= 1
    for cp in tl:
        dd[cp] += 1
    return r

r = 0
for i in range(N):
    ff = ss[i]
    dd[ff]-=1
    for j in range(i+1, N):
        sl = ss[j]
        x, y, z, w = sl
        dd[sl]-=1
        sls = [(x,y,z,w), (y,z,w,x), (z,w,x,y), (w,x,y,z)]
        for s in sls:
            r += f(ff, s)
        dd[sl]+=1
print(r)

from collections import defaultdict
N, = map(int, input().split())
dd = defaultdict(int)
cc = dict()
nrm = dict()
ss = []
for _ in range(N):
  xs = list(map(int, input().split()))
  cnd = [tuple(xs[j:]+xs[:j])for j in range(4)]
  x = min(cnd)
  for item in cnd:
    nrm[item] = x
  dd[x] += 1
  cc[x] = (4 if x[0]==x[1]else 2)if x[0]==x[2]and x[1]==x[3]else 1
  ss.append(x)
def f(ff, gg):
  a,b,c,d=ff
  e,h,g,f=gg
  tl = [(a,e,f,b), (b,f,g,c), (c,g,h,d), (d,h,e,a)]
  q = defaultdict(int)
  for p in tl:
    if p not in nrm:
      return 0
    q[nrm[p]] += 1
  r= 1
  for p, c in q.items():
    for i in range(c):
      r *= dd[p]-i
    r *= cc[p]**c
  return r
r = 0
for i in range(N):
  ff = ss[i]
  dd[ff]-=1
  for j in range(i+1, N):
    sl = ss[j]
    x, y, z, w = sl
    dd[sl]-=1
    r += sum(f(ff,tuple(sl[j:]+sl[:j]))for j in range(4))
    dd[sl]+=1
print(r)