import numpy as np def modinv(a: int, m: int) -> int: ''' モジュラ逆元 ax mod m =1の解x=a^(-1)を返す Parameters ---------- a:int m:int ''' x, y, u, v = 1, 0, 0, 1 M = m while m > 0: k = a//m x -= k*u y -= k*v x, u = u, x y, v = v, y a, m = m, a % m assert a == 1, "a and m aren't relatively prime numbers" if x < 0: x += M return x def mat_mul(a, b): """ a: 行列(2次元配列)I*J b: 行列(2次元配列)J*K """ I, J, K = len(a), len(b[0]), len(b) c = [[0] * J for _ in range(I)] for i in range(I): for j in range(J): for k in range(K): c[i][j] += a[i][k] * b[k][j] return c N, B = map(int, input().split()) A = [list(map(int, input().split())) for _ in range(3)] mat = np.matrix(A) det = np.linalg.det(mat) detn = pow(int(det), N, B) ans = modinv(detn, B) print(ans)