from math import gcd N, M = map(int, input().split()) P = [i for i in range(N)] for _ in range(M): L, *T = list(map(int, input().split())) sigma = [] for t in T: sigma.append(P[t-1]) for i in range(L): if i < L-1: j = T[i+1]-1 else: j = T[0]-1 P[j] = sigma[i] seen = [False]*N ans = 1 MOD = 998244353 for i in range(N): if seen[i]: continue tmp = 1 seen[i] = True next = P[i] while not seen[next]: seen[next] = True tmp += 1 next = P[next] ans = (ans*tmp//gcd(ans, tmp)) % MOD print(ans)