MOD = 998244353 def determinant(A): A = [a.copy() for a in A] n = len(A) res = 1 for i, row in enumerate(A): if row[i] == 0: for j in range(i + 1, n): if A[j][i] != 0: break else: return 0 A[i], A[j] = A[j], A[i] row = A[i] res *= -1 inv = pow(row[i], MOD - 2, MOD) for j in range(i + 1, n): a_j = A[j] t = a_j[i] * inv % MOD for k in range(i + 1, n): a_j[k] -= t * row[k] a_j[k] %= MOD for i in range(n): res *= A[i][i] res %= MOD return res n, m = map(int, input().split()) A = [[*map(int, input().split())] for _ in range(n)] B = [[*map(int, input().split())] for _ in range(m)] C = [[0] * n for i in range(n)] for k in range(m): for i in range(n): for j in range(n): C[i][j] += A[i][k] * B[k][j] for i in range(n): C[i][i] += 1 print(determinant(C))