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.15.1) (judge verdict: Accepted)

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}
type Tile = [usize; 4];
fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = AtRead::from(i);
    let n = i.read::<usize>();
    let mut c = Vec::new();
    for _ in 0..n {
        c.push(i.read_array::<[usize; 4], _>());
    }
    writeln!(o, "{}", solve(c)).unwrap();
}

fn solve(c: Vec<Tile>) -> usize {
    fn f(c: &mut [(Tile, usize)], cts: &[(Tile, usize); 4], n: usize) -> usize {
        if n == 4 {
            return 1;
        }
        let ct = cts[n];
        let mut result = 0;
        for i in 0..c.len() {
            if c[i].1 == 0 || c[i].0 != ct.0 {
                continue;
            }
            let count = c[i].1 * ct.1;
            c[i].1 -= 1;
            result += count * f(c, cts, n + 1);
            c[i].1 += 1;
        }
        result
    }
    fn normalize(mut c: Tile) -> (Tile, usize) {
        let mut c_min = c;
        let mut count = 1;
        for _ in 1..4 {
            c.rotate(1);
            if c < c_min {
                c_min = c;
                count = 1;
            } else if c == c_min {
                count += 1;
            }
        }
        (c_min, count)
    }
    let mut m = HashMap::new();
    for &c in &c {
        *m.entry(normalize(c).0).or_insert(0) += 1;
    }
    let mut c: Vec<_> = m.into_iter().collect();
    let mut result = 0;
    let mut cts = [([0; 4], 0); 4];
    for i0 in 0..c.len() {
        let c0 = c[i0];
        c[i0].1 -= 1;
        for i1 in 0..c.len() {
            let mut c1 = c[i1];
            if c1.1 == 0 {
                continue;
            }
            c1.0.reverse();
            c[i1].1 -= 1;
            for _ in 0..4 {
                for j0 in 0..4 {
                    let j1 = (j0 + 1) % 4;
                    cts[j0] = normalize([c0.0[j0], c1.0[j0], c1.0[j1], c0.0[j1]]);
                }
                result += c0.1 * c1.1 * f(&mut c, &cts, 0);
                c1.0.rotate(1);
            }
            c[i1].1 += 1;
        }
        c[i0].1 += 1;
    }
    result / 6
}

mod utils {
    use super::*;

    pub struct AtRead<R: BufRead> {
        r: R,
        s: String,
    }

    impl<R: BufRead> AtRead<R> {
        pub fn from(r: R) -> Self {
            AtRead {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }

        pub fn read_array<A, T>(&mut self) -> A
        where
            T: FromStr,
            A: AsMut<[T]> + Default,
        {
            let mut a: A = Default::default();
            {
                let a: &mut [T] = a.as_mut();
                let i = &mut self.read_line().split(' ');
                for n in 0..a.len() {
                    a[n] = next(i);
                }
            }
            a
        }
    }

    fn next<'a, T: FromStr, I: std::iter::Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

    pub trait AtSliceExt<T> {
        fn rotate(&mut self, mid: usize);
    }
    impl<T> AtSliceExt<T> for [T] {
        fn rotate(&mut self, mid: usize) {
            {
                let (l, r) = self.split_at_mut(mid);
                l.reverse();
                r.reverse();
            }
            self.reverse()
        }
    }
}