import math MOD = 998244353 N, M = map(int, input().split()) mapping = list(range(1, N+1)) for _ in range(M): S = list(map(int, input().split()))[1:] tmp = {mapping[S[(i+1)%len(S)]-1]: mapping[S[i]-1] for i in range(len(S))} for i in range(N): if mapping[i] in tmp: mapping[i] = tmp[mapping[i]] lst = list(range(1, N+1)) cnt = 0 while 1: for i in range(N): lst[i] = mapping[lst[i]-1] cnt += 1 for i in range(N): if lst[(i+1)%N] != ((lst[i] + 1) % (N + 1) if (lst[i] + 1) % (N + 1) != 0 else 1): break else: move = lst.index(1) break # cntで全体がmoveだけ動く if lst == list(range(1, N+1)): print(cnt) else: print((cnt * (N * move / (math.gcd(N, move)))) % MOD)