import std; const ulong MOD = 998244353; const ulong N = 10 ^^ 6; ulong[] primes; void init_primes(){ primes ~= 2; for(ulong p = 3; p < N; p += 2){ foreach(prime; primes){ if(p % prime == 0) break; if(prime ^^ 2 > p){ primes ~= p; break; } } } } ulong[][] prime_division(ulong n){ ulong[][] result; foreach(p; primes){ if(p > n) break; if(n % p) continue; result ~= [p, 0]; while(n % p == 0){ result[$ - 1][1] += 1; n /= p; } } return result; } ulong[][] pd_lcm(ulong[][] a, ulong[][] b){ ulong[][] result; auto i = 0; auto j = 0; while(true){ if(i >= a.length){ for(; j < b.length; j++){ result ~= b[j].dup; } break; } if(j >= b.length){ for(; i < a.length; i++){ result ~= a[i].dup; } break; } if(a[i][0] <= b[j][0]){ result ~= a[i].dup; if(a[i][0] == b[j][0]){ if(a[i][1] < b[j][1]){ result[$ - 1][1] = b[j][1]; } j++; } i++; }else{ result ~= b[j].dup; j++; } } return result; } ulong pd_long(ulong[][] a){ ulong result = 1; foreach(x; a){ result *= x[0].powmod(x[1], MOD); result %= MOD; } return result; } void main(){ auto input = readln.chomp.split.to!(int[]); int n = input[0]; int m = input[1]; auto s = new int[n]; for(auto i = 0; i < n; i++){ s[i] = i; } for(auto i = 0; i < m; i++){ input = readln.chomp.split.to!(int[]); int prev = s[input[$ - 1] - 1]; for(auto j = 1; j < input.length; j++){ auto t = s[input[j] - 1]; s[input[j] - 1] = prev; prev = t; } } init_primes; auto result = prime_division(1); auto closed = new bool[n]; for(auto i = 0; i < n; i++){ if(closed[i]) continue; auto j = i; ulong k = 0L; while(!closed[j]){ closed[j] = true; j = s[j]; k++; } result = result.pd_lcm(prime_division(k)); } writeln(result.pd_long); }