def extgcd(x,y): if y==0: return 1,0 r0,r1,s0,s1 = x,y,1,0 while r1 != 0: r0,r1, s0,s1 = r1,r0%r1, s1,s0-r0//r1*s1 return s0,(r0-s0*x)//y def modinv(a,MOD): x,y = extgcd(a,MOD) return x%MOD def divmod_poly(f,g,MOD): #MODは素数とする assert g != [] and g != [0] ainv = modinv(g[-1],MOD) df,dg = len(f),len(g) if df < dg: return [0], f[:] gg = [gi*ainv%MOD for gi in g] r = f[:] q = [0]*(df-dg+1) for i in range(df-dg,-1,-1): q[i] = c = r[-1] if c: for j in range(dg-1,-1,-1): r[j+i] -= c*gg[j] r[j+i] %= MOD r.pop() for i in range(df-dg+1): # g を monic にする q[i] = q[i]*ainv%MOD while r and r[-1]==0: r.pop() if not r: r = [0] return q,r def mul_poly(f,g,MOD): df,dg = len(f),len(g) res = [0]*(df+dg-1) for i in range(df): for j in range(dg): res[i+j] += f[i]*g[j] res[i+j] %= MOD return res def sub_poly(f,g,MOD): df,dg = len(f),len(g) m = max(df,dg) res = f[:]+[0]*(m-df) for i in range(m): if i < dg: res[i] -= g[i] if res[i] < 0: res[i] += MOD return res def bm(x,MOD): assert len(x)%2==0 L = len(x)//2 x.reverse() while x and x[-1]==0: x.pop() if not x: return [0] # all zero の場合 r0,r1,s0,s1 = x,[0]*(2*L-1)+[1],[1],[0] while len(s1) <= L and r1 != [0]: q,r = divmod_poly(r0,r1,MOD) #print(r0,r1,q,r) #assert mul_poly(q,r1,MOD)==sub_poly(r0,r,MOD) r0,r1 = r1,r s0,s1 = s1, sub_poly(s0,mul_poly(q,s1,MOD),MOD) #print(q,r0,r1,s0,s1) while s0 and s0[-1]==0: s0.pop() ainv = modinv(s0[-1],MOD) return [si*ainv%MOD for si in s0] def f(n,op,I): ans = 0 for p in range(1<>i&1,q>>i&1,I)<= 0 and R >= 0 #print(L,R,1<>0&1 if x==0 and y: return i>>1&1 if x and y==0: return i>>2&1 return 0 lst = [f(k,op,i) for k in range(1,7)] if n==0: ans += lst[0] continue #print(lst) a = bm(lst[:],MOD)[::-1] #for i in range(10): # x = rec_nth_term(lst[:len(a)-1],a,i) # print(lst,a,x) #for i,j in zip(lst,lst[1:]): # print(i*12-j*32) ans += rec_nth_term(lst[:len(a)-1],a,n-1) print(ans%MOD)