import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;

int[][string] cset;
int[][] cs;

void main(){
	int n = readln.chomp.to!int;
	
	foreach(i; 0 .. n){
		int[] x = readln.chomp.split.map!(to!int).array;
		cs ~= x;
		cset[x.map!(to!string).join(" ")] ~= i;
		x = x[1 .. $] ~ x[0];
		cset[x.map!(to!string).join(" ")] ~= i;
		x = x[1 .. $] ~ x[0];
		cset[x.map!(to!string).join(" ")] ~= i;
		x = x[1 .. $] ~ x[0];
		cset[x.map!(to!string).join(" ")] ~= i;
		}
	
	debug cset.writeln;
	
	cset.rehash;
	
	BigInt ans = 0;
	foreach(i; 0 .. n){
		int[] x = cs[i];
		foreach(j; 0 .. n){
			int[] y = cs[j] ~ [];
			ans += fn(x, y, i, j);
			y = y[1 .. $] ~ y[0];
			ans += fn(x, y, i, j);
			y = y[1 .. $] ~ y[0];
			ans += fn(x, y, i, j);
			y = y[1 .. $] ~ y[0];
			ans += fn(x, y, i, j);
			}
		}
	
	ans /= 6;
	ans.writeln;
	}

BigInt fn(int[] x, int[] y, int i, int j){
	if(i == j) return BigInt(0);
	BigInt ans;
	int[] u;
	int[] ps0, ps1, ps2, ps3;
	u = [x[0], y[0], y[3], x[1]];
	if(u.map!(to!string).join(" ") in cset) ps0 = cset[u.map!(to!string).join(" ")];
	else return BigInt(0);
	u = [x[1], y[3], y[2], x[2]];
	if(u.map!(to!string).join(" ") in cset) ps1 = cset[u.map!(to!string).join(" ")];
	else return BigInt(0);
	u = [x[2], y[2], y[1], x[3]];
	if(u.map!(to!string).join(" ") in cset) ps2 = cset[u.map!(to!string).join(" ")];
	else return BigInt(0);
	u = [x[3], y[1], y[0], x[0]];
	if(u.map!(to!string).join(" ") in cset) ps3 = cset[u.map!(to!string).join(" ")];
	else return BigInt(0);
	
	if(i == j) ans = 0;
	else foreach(p0; ps0){
		if(i == p0) continue;
		if(j == p0) continue;
		foreach(p1; ps1){
			if(i == p1) continue;
			if(j == p1) continue;
			if(p0 == p1) continue;
			foreach(p2; ps2){
				if(i == p2) continue;
				if(j == p2) continue;
				if(p0 == p2) continue;
				if(p1 == p2) continue;
				foreach(p3; ps3){
					if(i == p3) continue;
					if(j == p3) continue;
					if(p0 == p3) continue;
					if(p1 == p3) continue;
					if(p2 == p3) continue;
					
					ans += 1;
					}
				}
			}
		}
	
	debug if(ans > 0) writeln(x, " ", y, " ", i, " ", j, " ", ans);
	return ans;
	}