import sys from math import gcd def main(): MOD = 998244353 input = sys.stdin.read().split() ptr = 0 N, M = int(input[ptr]), int(input[ptr + 1]) ptr += 2 Q = list(range(N + 1)) # Q[0] is unused for _ in range(M): T = int(input[ptr]) s_list = list(map(int, input[ptr + 1:ptr + 1 + T])) ptr += T + 1 # Create the predecessor mapping for this cycle prev = {} prev[s_list[0]] = s_list[-1] for i in range(1, T): prev[s_list[i]] = s_list[i - 1] # Compute new values for each element in the cycle new_vals = {} for y in s_list: new_vals[y] = Q[prev[y]] # Update Q with new values for y in s_list: Q[y] = new_vals[y] # Build the forward permutation P from Q P = [0] * (N + 1) for y in range(1, N + 1): x = Q[y] P[x] = y # Compute cycle decomposition and LCM of cycle lengths visited = [False] * (N + 1) current_lcm = 1 for x in range(1, N + 1): if not visited[x]: cycle_len = 0 y = x while not visited[y]: visited[y] = True y = P[y] cycle_len += 1 # Update LCM g = gcd(current_lcm, cycle_len) current_lcm = (current_lcm * cycle_len) // g print(current_lcm % MOD) if __name__ == '__main__': main()