import bisect import copy import decimal import fractions import functools import heapq import itertools import math import random import sys from collections import Counter,deque,defaultdict from functools import lru_cache,reduce from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max def _heappush_max(heap,item): heap.append(item) heapq._siftdown_max(heap, 0, len(heap)-1) def _heappushpop_max(heap, item): if heap and item < heap[0]: item, heap[0] = heap[0], item heapq._siftup_max(heap, 0) return item from math import degrees, gcd as GCD read=sys.stdin.read readline=sys.stdin.readline readlines=sys.stdin.readlines def Extended_Euclid(n,m): stack=[] while m: stack.append((n,m)) n,m=m,n%m if n>=0: x,y=1,0 else: x,y=-1,0 for i in range(len(stack)-1,-1,-1): n,m=stack[i] x,y=y,x-(n//m)*y return x,y class MOD: def __init__(self,mod): self.mod=mod def Pow(self,a,n): a%=self.mod if n>=0: return pow(a,n,self.mod) else: assert math.gcd(a,self.mod)==1 x=Extended_Euclid(a,self.mod)[0] return pow(x,-n,self.mod) def Build_Fact(self,N): assert N>=0 self.factorial=[1] for i in range(1,N+1): self.factorial.append((self.factorial[-1]*i)%self.mod) self.factorial_inv=[None]*(N+1) self.factorial_inv[-1]=self.Pow(self.factorial[-1],-1) for i in range(N-1,-1,-1): self.factorial_inv[i]=(self.factorial_inv[i+1]*(i+1))%self.mod return self.factorial_inv def Fact(self,N): return self.factorial[N] def Fact_Inv(self,N): return self.factorial_inv[N] def Comb(self,N,K): if K<0 or K>N: return 0 s=self.factorial[N] s=(s*self.factorial_inv[K])%self.mod s=(s*self.factorial_inv[N-K])%self.mod return s class Lagrange_Interpolation: def __init__(self,X=False,Y=False,x0=None,xd=None,mod=0): self.degree=len(Y)-1 self.mod=mod assert self.degree0 self.X=[(x0+i*xd)%self.mod for i in range(self.degree+1)] fact_inve=1 for i in range(1,self.degree+1): fact_inve*=i*xd fact_inve%=self.mod fact_inve=MOD(self.mod).Pow(fact_inve,-1) self.coefficient=[y for y in Y] for i in range(self.degree-1,-1,-2): self.coefficient[i]*=-1 for i in range(self.degree,-1,-1): self.coefficient[i]*=fact_inve self.coefficient[i]%=self.mod self.coefficient[self.degree-i]*=fact_inve self.coefficient[self.degree-i]%=self.mod fact_inve*=i*xd fact_inve%=self.mod else: self.X=X assert len(self.X)==self.degree+1 self.coefficient=[y for y in Y] for i in range(self.degree+1): for j in range(self.degree+1): if i==j: continue self.coefficient[i]*=X[i]-X[j] self.coefficient%=self.mod def __getitem__(self,N): N%=self.mod XX=[N-x for x in self.X] XX_left=[1]*(self.degree+2) for i in range(1,self.degree+2): XX_left[i]=XX_left[i-1]*XX[i-1]%self.mod XX_right=[1]*(self.degree+2) for i in range(self.degree,-1,-1): XX_right[i]=XX_right[i+1]*XX[i]%self.mod return sum(XX_left[i]*XX_right[i+1]*self.coefficient[i] for i in range(self.degree+1))%self.mod def NTT(polynomial1,polynomial2): prim_root=3 prim_root_inve=MOD(mod).Pow(prim_root,-1) def DFT(polynomial,inverse=False): dft=polynomial+[0]*((1<>bit,mod) U=[1] for _ in range(a): U.append(U[-1]*x%mod) for i in range(1<>bit,mod) U=[1] for _ in range(a): U.append(U[-1]*x%mod) for i in range(1<pp: break if pp%d==0: divisors.append(d) while pp%d==0: pp//=d if pp>1: divisors.append(pp) primitive_root=2 while True: for d in divisors: if pow(primitive_root,(p-1)//d,p)==1: break else: return primitive_root primitive_root+=1 class Polynomial: def __init__(self,polynomial,max_degree=-1,eps=1e-12,mod=0): self.max_degree=max_degree if self.max_degree!=-1 and len(polynomial)>self.max_degree+1: self.polynomial=polynomial[:self.max_degree+1] else: self.polynomial=polynomial self.mod=mod self.eps=eps def __eq__(self,other): if type(other)!=Polynomial: return False if len(self.polynomial)!=len(other.polynomial): return False for i in range(len(self.polynomial)): if abs(self.polynomial[i]-other.polynomial[i])>self.eps: return False return True def __ne__(self,other): if type(other)!=Polynomial: return True if len(self.polynomial)!=len(other.polynomial): return True for i in range(len(self.polynomial)): if abs(self.polynomial[i]-other.polynomial[i])>self.eps: return True return False def __add__(self,other): assert type(other)==Polynomial summ=[0]*max(len(self.polynomial),len(other.polynomial)) for i in range(len(self.polynomial)): summ[i]+=self.polynomial[i] for i in range(len(other.polynomial)): summ[i]+=other.polynomial[i] if self.mod: for i in range(len(summ)): summ[i]%=self.mod while summ and abs(summ[-1])self.max_degree+1: prod=prod[:self.max_degree+1] while prod and abs(prod[-1])n for i in range(n): assert abs(self.polynomial[i])