""" """ import sys from sys import stdin N,M = map(int,stdin.readline().split()) p = [i for i in range(N)] for lp in range(M): ts = list(map(int,stdin.readline().split())) s = [] for j in range(1,len(ts)): s.append(ts[j]-1) s.reverse() pz = p[s[0]] for i in range(len(s)-1): p[s[i]] = p[s[i+1]] p[s[-1]] = pz #print (p) end = [False] * N c = [] for i in range(N): if not end[i]: v = i now = 0 while True: end[v] = True v = p[v] now += 1 if v == i: break c.append(now) #print (c,p) ans = 1 import math for i in c: ans = i * ans // math.gcd(i,ans) print (ans % 998244353)