import sys read = sys.stdin.buffer.read readline = sys.stdin.buffer.readline readlines = sys.stdin.buffer.readlines import numpy as np import itertools MOD = int(readline()) data = list(map(int,read().split())) A = np.int64(data[:4]).reshape(2,2) B = np.int64(data[4:]).reshape(2,2) def make_power_mat(A, L, MOD=MOD): N = A.shape[0] assert A.shape == (N,N) B = L.bit_length() x = np.empty((1<