import std; const ulong MOD = 998244353; 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; } } auto result = BigInt(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.lcm(BigInt(k)); } writeln(result % MOD); }