from collections import * from itertools import * from functools import * from heapq import * import sys,math input = sys.stdin.readline N,M = map(int,input().split()) class DSU: def __init__(self, n): self._n = n self.parent_or_size = [-1] * n def merge(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n x, y = self.leader(a), self.leader(b) if x == y: return x if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x self.parent_or_size[x] += self.parent_or_size[y] self.parent_or_size[y] = x return x def same(self, a, b): assert 0 <= a < self._n assert 0 <= b < self._n return self.leader(a) == self.leader(b) def leader(self, a): assert 0 <= a < self._n if self.parent_or_size[a] < 0: return a self.parent_or_size[a] = self.leader(self.parent_or_size[a]) return self.parent_or_size[a] def size(self, a): assert 0 <= a < self._n return -self.parent_or_size[self.leader(a)] def groups(self): leader_buf = [self.leader(i) for i in range(self._n)] result = [[] for _ in range(self._n)] for i in range(self._n): result[leader_buf[i]].append(i) return [r for r in result if r != []] class prime_factorize(): def __init__(self,M=10**6): self.sieve = [-1]*(M+1) self.sieve[1] = 1 self.p = [False]*(M+1) self.mu = [1]*(M+1) for i in range(2,M+1): if self.sieve[i] == -1: self.p[i] = True i2 = i**2 for j in range(i2,M+1,i2): self.mu[j] = 0 for j in range(i,M+1,i): self.sieve[j] = i self.mu[j] *= -1 def factors(self,x): tmp = [] while self.sieve[x] != x: tmp.append(self.sieve[x]) x //= self.sieve[x] tmp.append(self.sieve[x]) return tmp def is_prime(self,x): return self.p[x] def mobius(self,x): return self.mu[x] pf = prime_factorize() X = [i for i in range(N)] for _ in range(M): t,*S = map(int,input().split()) Y = [] for j in range(t): s0 = S[j]-1 # s1 = S[(j+1)%t]-1 Y.append(X[s0]) for j in range(t): s1 = S[(j+1)%t]-1 X[s1] = Y[j] # print(X) D = DSU(N) for i,s in enumerate(X): D.merge(i,s) X = [len(g) for g in D.groups()] V = defaultdict(int) for x in X: for k,v in Counter(pf.factors(x)).items(): V[k] = max(V[k],v) mod = 998244353 ans = 1 for k,v in V.items(): ans *= pow(k,v,mod) ans %= mod print(ans)