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][0] - 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] if 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]): z = det(b[0], b[2]) else: z = det(b[0], 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)