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 Rust (1.42.0) (judge verdict: Accepted)

// ---------- begin chmin, chmax ----------
trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------

use proconio::*;

#[fastout]
fn run() {
    input! {
        n: usize,
        a: [[u16; 4]; n],
    }
    let mut a: Vec<_> = a.into_iter().map(|a| [a[0], a[1], a[2], a[3]]).collect();
    let mut map = std::collections::BTreeMap::new();
    for a in a.iter_mut() {
        let mut x = *a;
        for _ in 0..4 {
            a.chmin(x.clone());
            x.rotate_left(1);
        }
        *map.entry(a.clone()).or_insert(0u64) += 1;
    }
    let mut ans = 0;
    for (i, f) in a.iter().enumerate() {
        *map.get_mut(f).unwrap() -= 1;
        for down in a.iter().skip(i + 1) {
            *map.get_mut(down).unwrap() -= 1;
            let mut t = [down[1], down[0], down[3], down[2]];
            for _ in 0..4 {
                let mut memo = std::collections::BTreeMap::new();
                let x = [f[3], f[2], f[1], f[0], f[3]];
                let y = [t[3], t[2], t[1], t[0], t[3]];
                for (x, y) in x.windows(2).zip(y.windows(2)) {
                    let mut p = [x[0], x[1], y[1], y[0]];
                    let mut q = p.clone();
                    for _ in 0..4 {
                        p.chmin(q.clone());
                        q.rotate_left(1);
                    }
                    *memo.entry(p).or_insert(0) += 1;
                }
                let mut val = 1;
                for (p, c) in memo {
                    let mut q = p.clone();
                    let mut v = 0;
                    for _ in 0..4 {
                        if p == q {
                            v += 1;
                        }
                        q.rotate_left(1);
                    }
                    let a = map.get(&p).cloned().unwrap_or(0);
                    for i in 0..c {
                        val *= (a - i) * v;
                    }
                }
                ans += val;
                t.rotate_left(1);
            }
            *map.get_mut(down).unwrap() += 1;
        }
    }
    println!("{}", ans);
}

fn main() {
    run();
}