from collections import defaultdict import sys input = sys.stdin.readline def prime_factor(n): factors = {} if n % 2 == 0: cnt = 0 while n % 2 == 0: cnt += 1 n //= 2 factors[2] = cnt i = 3 while i * i <= n: if n % i == 0: cnt = 0 while n % i == 0: cnt += 1 n //= i factors[i] = cnt i += 2 if n != 1: factors[n] = 1 return factors mod = 998244353 N, M = map(int, input().split()) P = list(range(N)) Q = list(range(N)) for _ in range(M): T, *S = map(int, input().split()) S = list(map(lambda x: x-1, S)) tmp = Q[P[S[0]]] for i in range(T-1): Q[P[S[i]]] = Q[P[S[i+1]]] Q[P[S[T-1]]] = tmp for i in range(N): P[Q[i]] = i visited = [0] * N exp = defaultdict(int) for i in P: if visited[i]: continue j = P[i] cnt = 1 while j != i: j = P[j] cnt += 1 for p, c in prime_factor(cnt).items(): exp[p] = max(exp[p], c) ans = 1 for p, e in exp.items(): ans = ans * pow(p, e, mod) % mod print(ans)