import sys from math import lcm from collections import deque input = sys.stdin.readline N, M = map(int, input().split()) P = list(range(N)) for _ in range(M): T, *S = map(int, input().split()) S.reverse() for i, j in zip(S, S[1:]): i -= 1 j -= 1 P[i], P[j] = P[j], P[i] cycle = deque() visited = [False] * N for i in range(N): if visited[i]: continue L = 0 while not visited[i]: visited[i] = True i = P[i] L += 1 cycle.append(L) while len(cycle) >= 2: x = cycle.popleft() y = cycle.popleft() cycle.append(lcm(x, y)) print(cycle[0] % 998244353)