#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; int N,M; ll mod = 998244353; vector G[1010101]; //ループの回数は適宜修正 ll power(ll a, ll b, ll m) { ll p = a, ans = 1; for(int i = 0; i < 60; i++) { ll wari = (1LL << i); if((b / wari) % 2 == 1) { ans = (ans * p) % m; } p = (p * p) % m; } return ans; } int main() { cin >> N >> M; vector P(N+1); for(int i = 1; i <= N; ++i) P[i] = i; for(int i = 0; i < M; ++i) { int t; cin >> t; vector v(t); for(int j = 0; j < t; ++j) cin >> v[j]; for(int j = t-1; j >= 1; --j) swap(P[v[j]], P[v[j-1]]); } // for(int i = 1; i <= N; ++i) cout << P[i] << " "; // cout << endl; for(int i = 1; i <= N; ++i) { if(P[i] != i) G[P[i]].push_back(i); } vector vis(N+1, false); set st; for(int i = 1; i <= N; ++i) { if(vis[i]) continue; vis[i] = true; int now = i; int cnt = 1; while(P[now] != i) { cnt++; now = P[now]; vis[now] = true; } if(cnt >= 2) st.insert(cnt); } // for(int v : st) cout << v << " "; // cout << endl; map mp; for(int v : st) { int tmp = v; for(int i = 2; i*i <= v; ++i) { int cnt = 0; while(tmp%i == 0) { cnt++; tmp/=i; } if(cnt>0) mp[i] = max(mp[i],cnt); if(tmp == 1) break; } if(tmp != 1) mp[tmp] = max(mp[tmp],1); } ll ans = 1; for(auto [k,v] : mp) { ans *= power(k, v, mod); ans %= mod; } cout << ans << endl; }