MOD = 998244353 import collections def prime_factorize(n): a = [] while n % 2 == 0: a.append(2) n //= 2 f = 3 while f * f <= n: if n % f == 0: a.append(f) n //= f else: f += 2 if n != 1: a.append(n) return a def n_fact(N): c = collections.Counter(prime_factorize(N)) return c def root(x): if P[x] < 0: return x P[x] = root(P[x]) # 経路圧縮 return P[x] def unite(x, y): x = root(x) y = root(y) if x == y: return if x > y: x, y = y, x P[x] += P[y] P[y] = x def same(x, y): return root(x) == root(y) def size(x): x = root(x) return -P[x] N, M = map(int, input().split()) P = [-1] * N P1 = list(range(N)) for _ in range(M): t, *S = map(int, input().split()) s0 = P1[S[0] - 1] n = len(S) for i in reversed(range(n)): if i == 0: P1[S[(i + 1) % t] - 1] = s0 continue P1[S[(i + 1) % t] - 1] = P1[S[i] - 1] for i, p in enumerate(P1): unite(i, p) from collections import defaultdict dic = defaultdict(int) for i in range(N): if i == root(i): s = size(i) p = n_fact(s) for k,v in p.items(): dic[k] = max(dic[k],p[k]) ans = 1 for k,v in dic.items(): ans *= pow(k,v,MOD) ans %= MOD print(ans)