import sys, time, random from collections import deque, Counter, defaultdict input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) inf = 2 ** 63 - 1 mod = 998244353 class dsu(): n=1 parent_or_size=[-1 for i in range(n)] def __init__(self,N): self.n=N self.num = N self.parent_or_size=[-1 for i in range(N)] def merge(self,a,b): assert 0<=a0: result2.append(result[i]) return result2 def count(self): return self.num n, m = mi() from math import gcd p = list(range(n)) TA = [li() for _ in range(m)] for x in range(m): t, *a = TA[x] a = [v - 1 for v in a] q = deque() for i in range(t): q.append(p[a[i]]) q.appendleft(q.pop()) for i in range(t): p[a[i]] = q.popleft() U = dsu(n) for i in range(n): U.merge(i, p[i]) G = sorted([len(v) for v in U.groups()], reverse=True) stack = list(G) while len(stack) >= 2: p = stack.pop() q = stack.pop() stack.append(p * q // gcd(p, q)) print(stack[0] % 998244353)