import sys
input = sys.stdin.readline

mod=998244353

N,M=map(int,input().split())

P=list(range(N))
for i in range(M):
    A=list(map(int,input().split()))[1:]
    X=[]

    for a in A:
        X.append(P[a-1])

    for i in range(len(A)):
        P[A[i]-1]=X[i-1]

    #print(P)

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

for i in range(N):
    Union(i,P[i])

from math import gcd

def lcm(x,y):
    return x*y//gcd(x,y)

ANS=1
for i in range(N):
    if find(i)==i:
        ANS=lcm(ANS,Nodes[i])

print(ANS%mod)