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] loop = [] seen = [False]*N 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] loop.append(tmp) ans = 1 MOD = 998244353 for c in loop: ans = (ans*c//gcd(ans, c)) % MOD print(ans)