#int(input()) #map(int, input().split()) #list(map(int, input().split())) N, M = map(int, input().split()) T = [0] * M S = [0] * M for i in range(M): a = list(map(int, input().split())) T[i] = a[0] for j in range(a[0]): a[j+1] -= 1 S[i] = a[1:] mod = 998244353 # print(S[i]) l = list(range(N)) nl = list(range(N)) for i in range(M): t = T[i] for j in range(t): nl[S[i][(j)%t]] = l[S[i][(j-1)%t]] l = list(nl) # print(l) v = [-1] * N u = [] # print(l) for i in range(N): if v[i] == -1: q = l[i] c = 1 v[i] = 1 while q != i: v[q] = 1 c += 1 q = l[q] u.append(c) # print(i, v, u) # print(l) # print(u) 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 from collections import Counter ps = dict() for x in u: c = Counter(prime_factorize(x)) for k, v in c.items(): if k not in ps: ps[k] = v else: ps[k] = max(ps[k], v) # print(ps) ans = 1 for k, v in ps.items(): ans = (ans * pow(k, v, mod)) % mod print(ans)