#include #include #include using namespace std; using ll = long long int; typedef bitset<1 << 28> bs; class dice_set { private: int multiset; ll partition; int get_partition(int index) { return partition >> 5 * index & 0b11111; } public: dice_set() : dice_set(0b11111111, 0b01001'01000'00111'00110'00101'00100'00011'00010'00001'00000ll) {} dice_set(int multiset_, ll partition_) : multiset(multiset_), partition(partition_) {} ll multichoose(ll *factorial) { ll ans = factorial[get_partition(9) - 9]; for (int i = 0;i < 9;++ i) ans /= factorial[get_partition(i + 1) - get_partition(i) - 1]; return ans; } void next(vector &dice, bs &uniquecheck, vector &que) { for (int result : dice) { int mask = (1 << get_partition(result)) - 1; int nextset = (multiset & ~mask) << 1 | (multiset & mask); if (uniquecheck[nextset]) continue; uniquecheck.set(nextset); ll next_partition = partition + (0b00001'00001'00001'00001'00001'00001'00001'00001'00001'00001ll & ~((0x20ll << result * 5) - 1)); que.push_back(dice_set(nextset, next_partition)); } } }; int main() { int N; cin >> N; vector> S(N, vector(6)); for (int i = 0;i < N;++ i) for (int j = 0;j < 6;++ j) { cin >> S[i][j]; -- S[i][j]; } ll factorial[21] = {1}; for (int i = 1;i <= 20;++ i) factorial[i] = factorial[i - 1] * i; vector now_queue(1, dice_set()); for (vector dice : S) { bs uniquecheck(0); vector next_queue; for (auto d : now_queue) d.next(dice, uniquecheck, next_queue); now_queue = next_queue; } int ans = 0; for (auto d : now_queue) ans = (int)((ans + d.multichoose(factorial)) % 998'244'353); cout << ans << endl; }