#include #include #include using namespace std; using lint = long long; template std::vector sort_unique(std::vector vec) { sort(vec.begin(), vec.end()), vec.erase(unique(vec.begin(), vec.end()), vec.end()); return vec; } using Array = lint; int main() { cin.tie(nullptr), ios::sync_with_stdio(false); int N; cin >> N; vector facs(21, 1); for (int i = 1; i < int(facs.size()); ++i) facs[i] = facs[i - 1] * i; vector states; { Array arr = 0; states.push_back(arr); } while (N--) { vector states_nxt; vector S(6); for (auto &x : S) cin >> x; S = sort_unique(S); for (auto &x : S) --x; for (auto &v : states) { for (int x : S) { v += 1LL << (5 * x); states_nxt.push_back(v); v -= 1LL << (5 * x); } } states = sort_unique(states_nxt); } lint ret = 0; for (auto vec : states) { int len = 0; lint den = 1; while (vec) { int d = vec % 32; den *= facs[d]; len += d; vec >>= 5; } ret += facs[len] / den; ret %= 998244353; } cout << ret << endl; }