MOD = 998244353 N,M = map(int,input().split()) S = [[] for _ in range(M)] for i in range(M): T = list(map(int,input().split())) for j in range(len(T)): T[j] -= 1 S[i] = T[1:] #置換を作成する F = [i for i in range(N)] inverse = [i for i in range(N)] for i in range(M): s = len(S[i]) for j in range(s-1): F[inverse[S[i][j]]] = S[i][j+1] F[inverse[S[i][s-1]]] = S[i][0] tmp = inverse[S[i][s-1]] for j in range(s-2,-1,-1): inverse[S[i][j+1]] = inverse[S[i][j]] inverse[S[i][0]] = tmp #サイクルの個数を考える visited = [False]*N C = [] for i in range(N): if visited[i]: continue now = F[i]; num = 1 visited[i] = True while now != i: visited[now] = True now = F[now] num += 1 C.append(num) #CのLCMを求める Map = {} for c in C: E = [] k = c i = 2 while i*i<=c: if k%i==0: E.append([i,0]) while k%i==0: k = k//i E[-1][1] += 1 i += 1 if k != 1: E.append([k,1]) for p in E: if not p[0] in Map: Map[p[0]] = 0 Map[p[0]] = max(Map[p[0]],p[1]) Ans = 1 for p in list(Map.items()): for i in range(p[1]): Ans = (Ans*p[0])%MOD #答えを出力する print(Ans)