#include using namespace std; using ll = long long; template using vec = vector; template using arr = array; int main(){ vec A, S; ll N, M, T; vec L; cin >> N >> M; for (ll n = 0; n <= N; n++) A.emplace_back(n); for (ll m = 0; m < M; m++) { cin >> T; S.resize(T); for (ll t = 0; t < T; t++) cin >> S[t]; S.emplace_back(0); for (ll t = T - 1; t >= 0; t--) A[S[t + 1]] = A[S[t]]; A[S[0]] = A[0]; } for (ll n = 1; n <= N; n++) if (A[n] != n) { ll now = n, d = 1, next; while (A[now] != n) { d++; next = A[now]; A[now] = now; now = next; } A[now] = now; L.emplace_back(d); } vec P(1001, true); P[0] = P[1] = false; for (ll n = 2; n <= 1000; n++) if (P[n]) for (ll t = 2 * n; t <= 1000; t += n) P[t] = false; vec ans_fac(N + 1, 0); for (auto e : L) { ll rt = sqrt(e); for (ll p = 2; p <= rt; p++) if (P[p]) { ll c = 0; while (e % p == 0) c++, e /= p; ans_fac[p] = max(ans_fac[p], c); } ans_fac[e] = max(ans_fac[e], 1LL); } ll ans = 1; for (ll n = 1; n <= N; n++) for (ll i = 0; i < ans_fac[n]; i++) ans = (ans * n) % 998244353; cout << ans << endl; return 0; }