import math n,m=map(int,input().split()) P=[i if i else None for i in range(n+1)] for j in range(m): S=[int(_) for _ in input().split()] t=P[S[-1]] for k in range(1,S[0]): P[S[-k]]=P[S[-1-k]] P[S[1]]=t #print(P) A=[-1]*(n+1) for i in range(1,n+1): if A[i]<0: S={i} j=i c=0 while True: j=P[j] c+=1 S=S|{j} if j==i: break for k in S: A[k]=c print(math.lcm(*A[1:])%998244353)