import sys def main(): MOD = 998244353 input = sys.stdin.read().split() ptr = 0 n, m = int(input[ptr]), int(input[ptr+1]) ptr += 2 cycles = [] for _ in range(m): t = int(input[ptr]) s = list(map(int, input[ptr+1:ptr+1+t])) ptr += 1 + t cycles.append(s) # Initialize permutation F as identity F = list(range(n + 1)) # 1-based indexing # Process each cycle in reverse order for s in reversed(cycles): k = len(s) # Create next_s for each element in the cycle next_s = [0] * k for i in range(k): next_s[i] = s[(i+1) % k] # Generate the temporary values temp = [] for i in range(k): temp.append(F[next_s[i]]) # Update F for each element in s for i in range(k): F[s[i]] = temp[i] # Find the cycle decomposition of F visited = [False] * (n + 1) lcm = 1 for i in range(1, n+1): if not visited[i]: length = 0 j = i while not visited[j]: visited[j] = True j = F[j] length += 1 # Compute LCM of lcm and length def gcd(a, b): while b: a, b = b, a % b return a current_gcd = gcd(lcm, length) lcm = (lcm // current_gcd) * length lcm %= MOD # Since LCM can be large, mod by MOD print(lcm % MOD) if __name__ == "__main__": main()