from collections import defaultdict class UnionFind(): def __init__(self, n): ''' UnionFindクラス。nは要素数を表す。 ''' self.n = n self.parents = [-1] * n def find(self, x): ''' 要素xを含む集合の親を見つける関数。 ''' if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y): ''' 要素xを含む集合と要素yを含む集合を合体する関数。 基本的には、要素数が多い集合に統合される。 要素数が同じときは要素yを含む集合に統合される。 ''' x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x def size(self, x): ''' 要素xを含む集合の要素数を出す関数。 ''' return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, x): root = self.find(x) return [i for i in range(self.n) if self.find(i) == root] def roots(self): return [i for i, x in enumerate(self.parents) if x < 0] def group_count(self): return len(self.roots()) def all_group_members(self): group_members = defaultdict(list) for member in range(self.n): group_members[self.find(member)].append(member) return group_members def __str__(self): return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items()) import math from functools import reduce mod = 998244353 def extgcd(a, b): """ 拡張ユークリッド互除法 ax + by = gcd(a,b) の最小整数解 (gcd(a,b), x, y) を返す Args: a (int): b (int): Returns: Tuple[int, int, int] """ u = y = 1 v = x = 0 while a: q = b // a x, u = u, x-q*u y, v = v, y-q*v b, a = a, b-q*a return b, x, y def lcm_base(x, y): ''' 最小公倍数を求める関数 ''' return (x * y % mod) * pow(extgcd(x, y)[0] % mod, mod - 2, mod) % mod def lcm_list(numbers): return reduce(lcm_base, numbers, 1) n, m = map(int, input().split()) P = [i for i in range(n + 1)] for _ in range(m): t, *S = map(int, input().split()) NP = P[:] for i in range(t): ci, ni = S[i], S[(i + 1) % t] NP[ni] = P[ci] P = NP[:] G = [1 for _ in range(n + 1)] UF = UnionFind(n + 1) for i in range(1, n + 1): if P[i] == i: continue j = P[i] if UF.same(i, j): continue i_root = UF.find(i) j_root = UF.find(j) UF.union(i, j) root = UF.find(i) if root == i_root: G[i_root] += G[j_root] G[j_root] = 0 else: G[j_root] += G[i_root] G[i_root] = 0 N = [] for i in range(1, n + 1): if G[i] == 0: continue N.append(G[i]) print(lcm_list(N))