#include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; class Prime { public: vector prime_list; const ll MAX_N = 1000000; Prime() { bool checked[MAX_N + 1]; memset(checked, false, sizeof(checked)); for (ll i = 2; i <= MAX_N; ++i) { if (!checked[i]) { prime_list.push_back(i); for (ll j = i * i; j <= MAX_N; j += i) { checked[j] = true; } } } } map prime_division(ll n) { map res; for (ll i = 0; prime_list[i] <= sqrt(n); ++i) { ll p = prime_list[i]; while (n % p == 0) { ++res[p]; n /= p; } } if (n != 1) { res[n] = 1; } return res; } bool is_prime(ll n) { if (n <= 1) return false; for (int i = 0; i < prime_list.size(); ++i) { if (n % prime_list[i]) return false; } return true; } }; int main() { int N, M; cin >> N >> M; int P[N]; for (int n = 0; n < N; ++n) { P[n] = n; } for (int i = 0; i < M; ++i) { int t; scanf("%d", &t); int S[t]; for (int j = 0; j < t; ++j) { scanf("%d", &S[j]); } int temp = P[S[0] - 1]; for (int j = 0; j < t; ++j) { int next = S[(j + 1) % t] - 1; int old = P[next]; P[next] = temp; temp = old; } } bool visited[N]; memset(visited, false, sizeof(visited)); vector nums; for (int v = 0; v < N; ++v) { if (visited[v]) continue; queue que; que.push(v); int size = 0; while (not que.empty()) { int x = que.front(); que.pop(); if (visited[x]) continue; visited[x] = true; ++size; que.push(P[x]); } nums.push_back(size); } ll MOD = 998244353; ll ans = 1; Prime prime; map values; for (ll x : nums) { map res = prime.prime_division(x); for (auto [n, e] : res) { values[n] = max(values[n], e); } } for (auto [n, e] : values) { ll v = 1; for (int i = 0; i < e; ++i) { v *= n; v %= MOD; } ans *= v; ans %= MOD; } cout << ans << endl; return 0; }