#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] u = l[S[i][t-2]] for j in range(t-2, -1, -1): l[S[i][(j)%t]] = l[S[i][(j-1)%t]] # l = list(nl) l[S[i][t-1]] = u 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) B = 10 ** 6 + 10 p = [-1] * B for i in range(2, B): if p[i] != -1: continue for j in range(B): if i * j >= B: break p[i*j] = i def prime_factorize(n): a = [] while n != 1: u = p[n] a.append(u) n //= u 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)