#include <iostream>
#include <vector>
#include <bitset>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <deque>
#define int long long

using namespace std;
typedef long long ll;
//____________________________________________
//____________________________________________
//__________________OOOOOOOOOOOOOOOO__________
//_________________OO_____________OO__________
//________________O_O____________O_O__________
//_______________O__O___________O__O__________
//______________O___O__________O___O__________
//_____________O____O_________O____O__________
//____________OOOOOOOOOOOOOOOO_____O__________
//____________O_____O________O_____O__________
//____________O_____OOOOOOOOOOOOOOOO__________
//____________O____O_________O____O___________
//____________O___O__________O___O____________
//____________O__O___________O__O_____________
//____________O_O____________O_O______________
//____________OO_____________OO_______________
//____________OOOOOOOOOOOOOOOO________________
//____________________________________________
//____________________________________________

const long long MOD = 1e9 + 7;
ll n;
ll c[500][4];
ll ans;
map<vector<int>, int> g;
map<vector<int>, int> cubes;

vector<int> reform(vector<int> c)
{
    vector<vector<int> > cc;
    for (int i = 0; i < 4; i++)
    {
        vector<int> x;
        for (int j = 0; j < 4; j++)
        {
            x.push_back(c[(i + j) % 4]);
        }
        cc.push_back(x);
    }
    sort(cc.begin(), cc.end());
    return *min_element(cc.begin(), cc.end());
}

vector<int> cube(vector<int> c)
{
    vector<vector<int> > cc;
    for (int i = 0; i < 8; i++)
    {
        vector<int> x;
        for (int j = 0; j < 8; j++)
        {
            x.push_back(c[(i + j) % 8]);
        }
        cc.push_back(x);
    }
    return *min_element(cc.begin(), cc.end());
}

ll cnk(ll k, ll n)
{
    if (k > n)
        return 0;
    if (k == 0)
        return 1;
    if (k == 1)
        return n;
    if (k == 2)
        return n * (n - 1);
    if (k == 3)
        return n * (n - 1) * (n - 2);
    if (k == 4)
        return n * (n - 1) * (n - 2) * (n - 3);
}

signed main()
{
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> c[i][0] >> c[i][1] >> c[i][2] >> c[i][3];
        vector<int> u = reform({c[i][0], c[i][1], c[i][2], c[i][3]});
        //cout << u[0] << " " << u[1] << " " << u[2] << " " << u[3] << endl;
        g[reform({c[i][0], c[i][1], c[i][2], c[i][3]})]++;
    }
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            for (int o1 = 0; o1 < 4; o1++)
            {
                //     c[j][1] ---- c[j][0]
                //       /|            /|
                //      / |           / |
                //     /  c[j][2]    /  c[j][3]
                // c[i][o1] ---- c[i][o1 + 1]
                //    | /            |
                //    |/             |
                // c[i][o1 + 3] ---- c[i][o1 + 2]
                vector<int> f = reform({c[i][0], c[i][1], c[i][2], c[i][3]});
                vector<int> b = reform({c[j][0], c[j][1], c[j][2], c[j][3]});
                g[f]--;
                g[b]--;
                int ful = c[i][o1];
                int fur = c[i][(o1 + 1) % 4];
                int fdl = c[i][(o1 + 3) % 4];
                int fdr = c[i][(o1 + 2) % 4];

                int bul = c[j][1];
                int bur = c[j][0];
                int bdl = c[j][2];
                int bdr = c[j][3];

                //vector<int> cuber = cube({ful, fur, fdr, fdl, bdl, bdr, bur, bul});
                //if (cubes[cuber] == 1)
                //    continue;
                //cubes[cuber] = 1;

                vector<int> l = {ful, fdl, bdl, bul};
                vector<int> u = {fur, ful, bul, bur};
                vector<int> r = {fur, bur, bdr, fdr};
                vector<int> d = {fdl, fdr, bdr, bdl};

                ll swappers = 1;

                ll y1 = 0;
                for (int x = 0; x < 4; x++)
                {
                    int ok = 1;
                    for (int y = 0; y < 4; y++)
                    {
                        if (l[y] != l[(x + y) % 4])
                            ok = 0;
                    }
                    y1 += ok;
                }
                swappers *= y1;

                y1 = 0;
                for (int x = 0; x < 4; x++)
                {
                    int ok = 1;
                    for (int y = 0; y < 4; y++)
                    {
                        if (d[y] != d[(x + y) % 4])
                            ok = 0;
                    }
                    y1 += ok;
                }
                swappers *= y1;

                y1 = 0;
                for (int x = 0; x < 4; x++)
                {
                    int ok = 1;
                    for (int y = 0; y < 4; y++)
                    {
                        if (u[y] != u[(x + y) % 4])
                            ok = 0;
                    }
                    y1 += ok;
                }
                swappers *= y1;

                y1 = 0;
                for (int x = 0; x < 4; x++)
                {
                    int ok = 1;
                    for (int y = 0; y < 4; y++)
                    {
                        if (r[y] != r[(x + y) % 4])
                            ok = 0;
                    }
                    y1 += ok;
                }
                swappers *= y1;

                r = reform(r);
                l = reform(l);
                u = reform(u);
                d = reform(d);
                
                //cout << i << " " << j << " " << swappers << endl;

                map<vector<int>, int> gg2;
                gg2[r]++;
                gg2[l]++;
                gg2[u]++;
                gg2[d]++;

                for (auto it : gg2)
                {
                    swappers *= cnk(it.second, g[it.first]);
                }
                //cout << swappers << endl;
                //cout << i << " " << j << " " << swappers << endl;

                ans += swappers;
                g[f]++;
                g[b]++;
            }
        }
        g[reform({c[i][0], c[i][1], c[i][2], c[i][3]})]--;
    }
    cout << ans;
}


Measured memory_bytes: 159872