#include #include using namespace std; using mint = atcoder::modint998244353; int main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int N, M; cin >> N >> M; vector P(N); for(int i = 0; i < N; i++) { P[i] = i; } while(M--) { int T; cin >> T; vector S(T); for(int i = 0; i < T; i++) { cin >> S[i]; --S[i]; } int fi = P[S.back()]; for(int i = T - 1; i > 0; i--) { P[S[i]] = P[S[i - 1]]; } P[S.front()] = fi; //for(int i = 0; i < N; i++) cout << P[i] << " \n"[i == N - 1]; } bool ok = true; for(int i = 0; i < N; i++) { if(P[i] != i) { ok = false; } } if(ok) { cout << 1 << '\n'; } else { vector dist(N, false); map mp; for(int i = 0; i < N; i++) { if(!dist[i]) { int now = i; int cnt = 0; while(!dist[now]) { dist[now] = true; now = P[now]; cnt++; } if(cnt > 1) { int T = cnt; for(int j = 2; j * j <= cnt; j++) { if(T % j == 0) { int c = 0; while(T % j == 0) { c++; T /= j; } if(mp.count(j) == 0) { mp[j] = c; } else { mp[j] = max(mp[j], c); } } } if(T > 1) { if(mp.count(T) == 0) { mp[T] = 1; } } } } } mint ans = 1; for(auto [p, q] : mp) { ans *= mint(p).pow(q); } cout << ans.val() << '\n'; } return 0; }