#include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; using ll = long long; ll fact[21]; struct ms { int mulset; ll part; ms() { mulset = 511; part = 9043225708576ll; } ms(int mulset_, ll part_) : mulset(mulset_), part(part_) {} int getpart(int ind) { if (ind == -1) return -1; return part >> (5 * ind) & (31); } ll count() { ll ret = fact[getpart(8) - 8]; for (int i = 0; i < 9; i++) ret /= fact[getpart(i) - getpart(i - 1) - 1]; return ret; } }; int main() { int n; cin >> n; fact[0] = 1; for (int i = 1; i <= 20; i++) fact[i] = fact[i - 1] * i; vector dp(1, ms()), ndp; ndp.reserve(3200000); bitset<1 << 29> st(0); int S[6]; for (int i = 0; i < n; i++) { for (int j = 0; j < 6; j++) cin >> S[j], --S[j]; for (auto &v : dp) { for (int j = 0; j < 6; j++) { int mask = (1 << v.getpart(S[j])) - 1; int nms = (v.mulset & ~mask) << 1 | (v.mulset & mask); if (st[nms]) continue; st.set(nms); ll np = v.part + (1134979744801ll & ~((1ll << (5 * S[j])) - 1)); ndp.push_back(ms(nms, np)); } } swap(dp, ndp); ndp.clear(); ndp.reserve(3200000); } ll ans = 0; for (auto &tmp : dp) { ans = (ans + tmp.count()) % 998244353; } cout << ans << endl; }