from collections import defaultdict import sys input = sys.stdin.readline n, m = map(int, input().split()) P = [i for i in range(n + 1)] for _ in range(m): t, *S = map(int, input().split()) sigma = [] for i in range(t): sigma.append(P[S[i]]) for i in range(t): j = S[(i + 1) % t] P[j] = sigma[i] mod = 998244353 N = set() seen = set() for i in range(1, n + 1): if i in seen: continue cnt = 1 seen.add(i) Stack = [i] while Stack: cp = Stack.pop() np = P[cp] if np in seen: continue seen.add(np) cnt += 1 Stack.append(np) N.add(cnt) ans = 1 PrimeCnt = defaultdict(int) for num in N: if num % 2 == 0: cnt = 0 while num % 2 == 0: num //= 2 cnt += 1 PrimeCnt[2] = max(PrimeCnt[2], cnt) f = 3 while f * f <= num: if num % f == 0: cnt = 0 while num % f == 0: num //= f cnt += 1 PrimeCnt[f] = max(PrimeCnt[f], cnt) f += 2 if num != 1: PrimeCnt[num] = max(PrimeCnt[num], 1) ans = 1 for prime, cnt in PrimeCnt.items(): ans *= pow(prime, cnt, mod) ans %= mod print(ans)