import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### import sys input = lambda :sys.stdin.readline()[:-1] ni = lambda :int(input()) na = lambda :list(map(int,input().split())) yes = lambda :print("yes");Yes = lambda :print("Yes");YES = lambda : print("YES") no = lambda :print("no");No = lambda :print("No");NO = lambda : print("NO") ####################################################################### n, mod = na() a = [na() for i in range(3)] b = list(zip(*a)) z = a[0][0]*a[1][1]*a[2][2] + a[0][1]*a[1][2]*a[2][0] + a[0][2]*a[1][0]*a[2][1] - a[0][2]*a[1][1]*a[0][1] - a[0][1]*a[1][0]*a[2][2] - a[0][0]*a[1][2]*a[2][1] if z != 0: z = abs(z) print(pow(z, n, mod)) exit() def f(a, b): if a[0]*b[1] - a[1]*b[0] == 0 and a[0]*b[2] - a[2]*b[0] == 0: return 1 return 0 def det(a, b): return a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0] def mat_mul(A,B): assert len(A[0]) == len(B) n = len(A) m = len(B[0]) p = len(A[0]) R = [[0 for j in range(m)]for i in range(n)] for i in range(n): for j in range(m): for k in range(p): R[i][j] += A[i][k]*B[k][j] % mod R[i][j] %= mod return R e = [[1,0,0],[0,1,0],[0,0,1]] for i in range(n): e = mat_mul(e, a) a = e b = list(zip(*a)) if a[0].count(0) + a[1].count(0) + a[2].count(0) == 9: print(0) elif f(b[0], b[1]) and f(b[0], b[2]): if (a[0][0] >= 0 and a[1][0] >= 0 and a[2][0] >= 0) or (a[0][0] <= 0 and a[1][0] <= 0 and a[2][0] <= 0): print(0) else: print("infty") else: if f(b[0], b[1])^1: z = det(b[0], b[1]) elif f(b[0], b[1])^1: z = det(b[0], b[2]) else: z = det(b[1], b[1]) if (z[0] > 0 and z[1] > 0 and z[2] > 0) or (z[0] < 0 and z[1] < 0 and z[2] < 0): print("infty") else: print(0)