#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
const int mod = 1e9 + 7;
const int N = 401;
const int K = 5000;
const int b = 1000;
int c[N][4], a[N][4], dp[N][20], d[N], v[N], q[N][20];
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
        for (int j = 0; j < 4; j++){
            cin >> c[i][j];
           // c[i][j] = 0;
        }
        for (int j = 0; j < 4; j++){
            a[i][j] = b * b * b * c[i][(j) % 4] + b * b * c[i][(j + 1) % 4] + b * c[i][(j + 2) % 4] + c[i][(j + 3) % 4];
         //   cout << a[i][j] << " ";
        }
     //   cout << endl;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++){
        for (int j = i + 1; j <= n; j++){
            for (int r = 0; r < 4; r++){
              //  cout << endl;
                for (int k = 0; k < 4; k++) d[k] = c[j][(k + r) % 4];
                v[0] = c[i][0] * b * b * b + d[1] * b * b + d[0] * b + c[i][1];
                v[1] = d[1] * b * b * b + c[i][0] * b * b + c[i][3] * b + d[2];
                v[2] = c[i][2] * b * b * b + d[3] * b * b + d[2] * b + c[i][3];
                v[3] = c[i][1] * b * b * b + d[0] * b * b + d[3] * b + c[i][2];
               // cout << v[0] << " " << v[1] << " " << v[2] << " " << v[3] << endl;
                memset(dp, 0, sizeof dp);
                memset(q, 0, sizeof q);
                for (int l = i + 1; l <= n; l++){
                    for (int t = 0; t < 4; t++){
                        for (int k = 0; k < 4; k++){
                            if (a[l][k] == v[t]) q[l][t]++;
                        }
                    }
                }
                dp[i][0] = 1;
                for (int l = i + 1; l <= n; l++){
                    if (l != j){
                        dp[l][0] = dp[l - 1][0];
                        dp[l][1] = dp[l - 1][1] + q[l][0] * dp[l - 1][0];
                        dp[l][2] = dp[l - 1][2] + q[l][1] * dp[l - 1][0];
                        dp[l][3] = dp[l - 1][3] + q[l][1] * dp[l - 1][1] + q[l][0] * dp[l - 1][2];
                        dp[l][4] = dp[l - 1][4] + q[l][2] * dp[l - 1][0];
                        dp[l][5] = dp[l - 1][5] + q[l][2] * dp[l - 1][1] + q[l][0] * dp[l - 1][4];
                        dp[l][6] = dp[l - 1][6] + q[l][2] * dp[l - 1][2] + q[l][1] * dp[l - 1][4];
                        dp[l][7] = dp[l - 1][7] + q[l][2] * dp[l - 1][3] + q[l][1] * dp[l - 1][5] + q[l][0] * dp[l - 1][6];
                        dp[l][8] = dp[l - 1][8] + q[l][3] * dp[l - 1][0];
                        dp[l][9] = dp[l - 1][9] + q[l][3] * dp[l - 1][1] + q[l][0] * dp[l - 1][8];
                        dp[l][10] = dp[l - 1][10] + q[l][3] * dp[l - 1][2] + q[l][1] * dp[l - 1][8];
                        dp[l][11] = dp[l - 1][11] + q[l][3] * dp[l - 1][3] + q[l][1] * dp[l - 1][9] + q[l][0] * dp[l - 1][10];
                        dp[l][12] = dp[l - 1][12] + q[l][3] * dp[l - 1][4] + q[l][2] * dp[l - 1][8];
                        dp[l][13] = dp[l - 1][13] + q[l][3] * dp[l - 1][5] + q[l][2] * dp[l - 1][9] + q[l][0] * dp[l - 1][12];
                        dp[l][14] = dp[l - 1][14] + q[l][3] * dp[l - 1][6] + q[l][2] * dp[l - 1][10] + q[l][1] * dp[l - 1][12];
                        dp[l][15] = dp[l - 1][15] + q[l][3] * dp[l - 1][7] + q[l][2] * dp[l - 1][11] + q[l][1] * dp[l - 1][13] + q[l][0] * dp[l - 1][14];
                    } else{
                        dp[l][0] = dp[l - 1][0];
                        dp[l][1] = dp[l - 1][1];
                        dp[l][2] = dp[l - 1][2];
                        dp[l][3] = dp[l - 1][3];
                        dp[l][4] = dp[l - 1][4];
                        dp[l][5] = dp[l - 1][5];
                        dp[l][6] = dp[l - 1][6];
                        dp[l][7] = dp[l - 1][7];
                        dp[l][8] = dp[l - 1][8];
                        dp[l][9] = dp[l - 1][9];
                        dp[l][10] = dp[l - 1][10];
                        dp[l][11] = dp[l - 1][11];
                        dp[l][12] = dp[l - 1][12];
                        dp[l][13] = dp[l - 1][13];
                        dp[l][14] = dp[l - 1][14];
                        dp[l][15] = dp[l - 1][15];
                    }
                }
                //cout << "!! " << dp[n][15] << endl;
                ans += dp[n][15];
            }
        }
    }
    cout << ans;
}


Measured memory_bytes: 384