Language: C++

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

inline int GetBit(int mask, int num) {
    return (mask >> num) & 1;
}

int cFront[4];
int cBack[4];

inline bool CanLeft(int c0, int c1, int c2, int c3) {
    return c0 == cBack[3] && c1 == cBack[2] && c2 == cFront[1] && c3 == cFront[0];
}

inline bool CanRight(int c0, int c1, int c2, int c3) {
    return c0 == cFront[3] && c1 == cFront[2] && c2 == cBack[1] && c3 == cBack[0];
}

inline bool CanUp(int c0, int c1, int c2, int c3) {
    return c0 == cFront[1] && c1 == cBack[2] && c2 == cBack[1] && c3 == cFront[2];
}

inline bool CanDown(int c0, int c1, int c2, int c3) {
    return c0 == cBack[3] && c1 == cFront[0] && c2 == cFront[3] && c3 == cBack[0];
}

inline bool Can(int b, int c1, int c2, int c3, int c4) {
    if (b == 0) {
        return CanLeft(c1, c2, c3, c4);
    } else if (b == 1) {
        return CanRight(c1, c2, c3, c4);
    } else if (b == 2) {
        return CanUp(c1, c2, c3, c4);
    } else {
        return CanDown(c1, c2, c3, c4);
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int n;
    cin >> n;

    vector<vector<int>> colors(n, vector<int>(8));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < 4; ++j) {
            cin >> colors[i][j];
            colors[i][j + 4] = colors[i][j];
        }
    }

    /*
    vector<vector<int>> bits(1 << 4);
    for (int mask = 0; mask < (1 << 4); ++mask) {
        for (int b = 0; b < 4; ++b) {
            if (!GetBit(mask, b)) {
                bits[mask].push_back(b);
            }
        }
    }
    */

    long long result[2][1 << 4];

    int buffer[4];

    long long glResult = 0;
    for (int i = 0; i < n; ++i) {
        for (int t = 0; t < 4; ++t) {
            cFront[t] = colors[i][t];
        }

        for (int j = i + 1; j < n; ++j) {
            for (int dj = 0; dj < 4; ++dj) {
                for (int t = 0; t < 4; ++t) {
                    cBack[t] = colors[j][dj + t];
                }

                int curPtr = 0;
                memset(result[curPtr], 0, sizeof(long long) * (1 << 4));
                result[curPtr][0] = 1;

                for (int k = i + 1; k < n; ++k) {
                    if (k == j) {
                        continue;
                    }

                    const int nxtPtr = 1 - curPtr;
                    for (int t = 0; t < (1 << 4); ++t) {
                        result[nxtPtr][t] = result[curPtr][t];
                    }

                    memset(buffer, 0, sizeof(int) * 4);

                    for (int dk = 0; dk < 4; ++dk) {
                        /*
                        buffer[0] += CanLeft(colors[k][dk], colors[k][dk + 1], colors[k][dk + 2], colors[k][dk + 3]);
                        buffer[1] += CanRight(colors[k][dk], colors[k][dk + 1], colors[k][dk + 2], colors[k][dk + 3]);
                        buffer[2] += CanUp(colors[k][dk], colors[k][dk + 1], colors[k][dk + 2], colors[k][dk + 3]);
                        buffer[3] += CanDown(colors[k][dk], colors[k][dk + 1], colors[k][dk + 2], colors[k][dk + 3]);
                        */

                        for (int b = 0; b < 4; ++b) {
                            if (Can(b, colors[k][dk], colors[k][dk + 1], colors[k][dk + 2], colors[k][dk + 3])) {
                                ++buffer[b];
                            }
                        }
                    }

                    for (int mask = 0; mask < (1 << 4); ++mask) {
                        for (int b = 0; b < 4; ++b) {
                            if (!GetBit(mask, b)) {
                                result[nxtPtr][mask + (1 << b)] += buffer[b] * result[curPtr][mask];
                            }
                        }
                    }

                    curPtr = nxtPtr;
                }

                glResult += result[curPtr][(1 << 4) - 1];
            }
        }
    }

    cout << glResult << endl;

    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:85:17: error: 'memset' was not declared in this scope
   85 |                 memset(result[curPtr], 0, sizeof(long long) * (1 << 4));
      |                 ^~~~~~
a.cc:4:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    3 | #include <algorithm>
  +++ |+#include <cstring>
    4 | #include <string>