#include using namespace std; #define ll long long int main(){ int n,m; cin >> n >> m; vector a(n); iota(a.begin(), a.end(), 0); for(int i = 0; i < m; i++){ int t; cin >> t; vector s(t); for(int j = 0; j < t; j++){ int x; cin >> x; s[j] = x-1; } int p = s[0]; for(int k = 1; k < t; k++){ swap(a[s[k]], a[p]); } } vector l; vector seen(n, false); for(int i = 0; i < n; i++){ if(seen[i]) continue; seen[i] = true; int cur = a[i]; int len = 1; while(cur != i){ len++; seen[cur] = true; cur = a[cur]; } l.push_back(len); } map lcm; ll answer = 1; vector primes(1001, true); primes[0] = false; primes[1] = false; for(int i = 2; i < 1001;i++) if (primes[i]){ for(int j = 2*i; j < 1001; j += i) primes[j] = false; } for(ll x: l){ map cur; ll e = x; ll root = sqrt(x); for(int i = 2; i <= root; i++) { if(primes[i]){ while(e % i == 0) { cur[i]++; e /= i; } } } cur[e]++; for(auto& i: cur) if(lcm[i.first] < i.second) lcm[i.first] = i.second; } for(auto& i: lcm){ for(ll j = 0; j < i.second; j++){ answer = (answer*i.first) % 998244353; } } cout << answer; }