#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include #include #include using namespace std; using ll = long long int; typedef bitset<1 << 29> bs; // 64MB程度 int deBrujin32[32] = { 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8, 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9 }; int lsb(unsigned int i) { // 丁度1bit立っている値に対してその立っている位置を返す return deBrujin32[i * 0x077CB531 >> 27]; } ll calc_partition(int multiset) { // 与えられた多重集合に対して、立っているbitの位置を保持する数列Pを返す ll partition = 0; for (int i = 5; i <= 45; i += 5) { int lob = multiset & -multiset; partition += (1LL + lsb(lob)) << i; multiset -= lob; } return partition; } int get_partition(ll partition, int index) { // multiSetでindex番目に立っているbitの位置を求める return (int) (partition >> 5 * index & 0b11111); } ll multichoose(ll *factorial, int multiset) { // multiSetで与えられた多重集合を並べてできる組合せ ll partition = calc_partition(multiset); ll multichoose = factorial[get_partition(partition, 9) - 9]; for (int i = 0; i < 9; ++i) multichoose /= factorial[get_partition(partition, i + 1) - get_partition(partition, i) - 1]; return multichoose; } int next(int multiset, vector &dice, bs &check_unique, int *stack, int length) {// diceを追加したときにできる新たな多重集合のうち、新しく発見したものをstackに入れる ll partition = calc_partition(multiset); for (int result : dice) { int mask = (1 << get_partition(partition, result)) - 1; int next = (multiset & ~mask) << 1 | (multiset & mask); if (check_unique[next]) continue; check_unique.set(next); stack[length++] = next; } return length; } 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; int now_stack[3200000] = {0b111111111}, next_stack[3200000]; // 初項M_0を求める int now_index = 1, next_index = 0; bs unique_check(0); for (vector dice : S) { for (int i = 0;i < now_index;++ i) next_index = next(now_stack[i], dice, unique_check, next_stack, next_index); // M_iからM_{i+1}を求める swap(now_stack, next_stack); now_index = next_index; next_index = 0; } int ans = 0; for (int i = 0;i < now_index;++ i) ans = (int)((ans + multichoose(factorial, now_stack[i])) % 998'244'353); cout << ans << endl; }