def mod_perm_ryser(matrix, n, mod): perm = 0 for s in range(1, 1 << n): bits = bin(s).count('1') parity = (-1) ** (n - bits) sums = [0] * n for i in range(n): for j in range(n): if (s >> j) & 1: sums[i] = (sums[i] + matrix[i][j]) % mod product = 1 for x in sums: product = (product * x) % mod term = (parity * product) % mod perm = (perm + term) % mod return perm def mod_det_gauss(matrix, n, mod): det_mod = 1 sign = 1 mat = [row.copy() for row in matrix] for i in range(n): pivot = -1 for j in range(i, n): if mat[j][i] % mod != 0: pivot = j break if pivot == -1: return 0 if i != pivot: mat[i], mat[pivot] = mat[pivot], mat[i].copy() sign *= -1 a = mat[i][i] % mod det_mod = (det_mod * a) % mod try: inv_a = pow(a, -1, mod) except ValueError: return 0 for j in range(i + 1, n): factor = (mat[j][i] * inv_a) % mod for k in range(i, n): mat[j][k] = (mat[j][k] - factor * mat[i][k]) % mod result = (sign * det_mod) % mod return result n, B = map(int, input().split()) mod = 2 * B matrix = [] for _ in range(n): row = list(map(int, input().split())) matrix.append(row) s_mod = mod_perm_ryser(matrix, n, mod) d_mod = mod_det_gauss(matrix, n, mod) delta = (s_mod - d_mod) % mod if delta % 2 != 0: delta = (delta + mod) % mod # Ensure even o_mod = (delta // 2) % B print(o_mod)