#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 i = 1; i < t; i++){ swap(a[s[i]], 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; bool primes[1001] = {1}; primes[0] = false; primes[1] = false; for(int i = 2; i < 1001;i++) if (primes[i]){ for(int j = 2; j*i < 1001; j++) primes[j*i] = false; } for(int x: l){ map cur; int root = sqrt(x); for(int i = 2; i <= root; i++) { if(primes[i]){ while(x % i == 0) { cur[i]++; x /= i; } } } cur[x]++; for(auto& i: cur) if(lcm[i.first] < i.second) lcm[i.first] = i.second; } for(auto& i: lcm){ for(int j = 0; j < i.second; j++){ answer = (answer*i.first) % 998244353; } } cout << answer; }