import sys input = sys.stdin.readline N, K = map(int, input().split()) mod = 998244353 Edge = [[] for i in range(K)] for i in range(K): t = int(input()) for _ in range(t): a, b = map(int, input().split()) a, b = a - 1, b - 1 Edge[i].append((a, b)) def determinant(A, mod): ans = 1 N = len(A) - 1 for i in range(N): if A[i][i] == 0: for j in range(i + 1, N): if A[j][i] != 0: A[i], A[j] = A[j], A[i] ans *= -1 break else: return 0 ans *= A[i][i] ans %= mod inv = pow(A[i][i], mod - 2, mod) for j in range(i, N): A[i][j] *= inv A[i][j] %= mod for j in range(i + 1, N): x = A[j][i] for k in range(i, N): A[j][k] -= x * A[i][k] A[j][k] %= mod return ans K2 = 1 << K ans = 0 for s in range(1, K2): pm = (-1) ** K L = [[0] * N for i in range(N)] for i in range(K): if (s >> i) & 1: pm *= -1 for a, b in Edge[i]: L[a][a] += 1 L[b][b] += 1 L[a][b] -= 1 L[b][a] -= 1 ans += determinant(L, mod) * pm ans %= mod print(ans)