#include using namespace std; #if __has_include() #include #endif using ll=long long; using ull=unsigned long long; using P=pair; templateusing minque=priority_queue,greater>; templatebool chmax(T &a,const T &b){return (abool chmin(T &a,const T &b){return (a>b?(a=b,true):false);} templateistream &operator>>(istream &is,pair&p){is>>p.first>>p.second;return is;} templateistream &operator>>(istream &is,vector &a){for(auto &i:a)is>>i;return is;} templatevoid operator++(pair&a,int n){a.first++,a.second++;} templatevoid operator--(pair&a,int n){a.first--,a.second--;} templatevoid operator++(vector&a,int n){for(auto &i:a)i++;} templatevoid operator--(vector&a,int n){for(auto &i:a)i--;} #define reps(i,a,n) for(int i=(a);i(0) ll myceil(ll a,ll b){return (a+b-1)/b;} template auto vec(const int (&d)[n],const T &init=T()){ if constexpr (id(d,init)); else return init; } #ifdef LOCAL #include #else #define debug(...) static_cast(0) #define debugg(...) static_cast(0) templateostream &operator<<(ostream &os,const pair&p){os<>testcase; for(int i=0;i>=1; } return res; } constexpr int primitive_root_constexpr(int x){ if(x==167772161)return 3; if(x==469762049)return 3; if(x==754974721)return 11; if(x==880803841)return 26; if(x==998244353)return 3; if(x==2)return 1; int x2=x; int p[20]; int c=0; x--; for(int i=2;i*i<=x;i++){ if(x%i==0){ p[c++]=i; while(x%i==0)x/=i; } } if(x!=1)p[c++]=x; x=x2; for(int g=2;;g++){ bool ok=true; for(int i=0;i struct ntt_root{ static constexpr int rank2=lsb(m-1); static constexpr int g=primitive_root_constexpr(m); std::arrayroot,invroot; std::arrayrate2,invrate2; std::arrayrate3,invrate3; constexpr ntt_root(){ root[rank2]=pow_mod(g,m>>rank2,m); invroot[rank2]=pow_mod(root[rank2],m-2,m); for(int i=rank2-1;i>=0;i--){ root[i]=(long long)root[i+1]*root[i+1]%m; invroot[i]=(long long)invroot[i+1]*invroot[i+1]%m; } int prod=1,invprod=1; for(int i=0;i void dft(std::vector&a){ static constexpr ntt_rootr; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=0; while(len void idft(std::vector&a){ static constexpr ntt_rootr; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=h; while(len){ if(len==1){ int p=1<<(h-1); for(int i=0;i std::vectorntt_convolution(std::vector a,std::vector b){ int n=a.size(),m=b.size(),s=n+m-1; if(std::min(n,m)<60){ std::vectorret(s,0); if(nc(z); for(int i=0;i void transposed_dft(std::vector&a){ static constexpr ntt_rootr; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=h; while(len){ if(len==1){ int p=1<<(h-1); T rot=T::raw(1); for(int i=0;i void transposed_idft(std::vector&a){ static constexpr ntt_rootr; static constexpr unsigned long long mod2=(unsigned long long)T::mod()*T::mod(); int n=a.size(); int h=lsb(n); int len=0; while(len void ntt_doubling(std::vector&a){ static constexpr ntt_rootr; int n=a.size()/2; std::vectorb(a.begin(),a.begin()+n); idft(b); T now=T::raw(n).inv(),zeta=T::raw(r.root[msb(n)+1]); for(int i=0;i void transposed_ntt_doubling(std::vector&a){ static constexpr ntt_rootr; int n=a.size()/2; std::vectorb(a.begin(),a.begin()+n); a=std::vector(a.begin()+n,a.end()); transposed_dft(a); T now=T::raw(n).inv(),zeta=T::raw(r.root[msb(n)+1]); for(int i=0;i template struct mod_int{ private: static constexpr unsigned int umod=static_cast(m); using uint=unsigned int; using mint=mod_int; uint v; static_assert(m,std::nullptr_t> =nullptr> mod_int(T a){ a%=m; if(a<0)v=a+umod; else v=a; } template,std::nullptr_t> =nullptr> mod_int(T a):v(a%umod){} static constexpr mint raw(int a){ mint ret; ret.v=a; return ret; } inline uint val()const{return this->v;} static constexpr int mod(){return m;} inline mint &operator+=(const mint &b){ this->v+=b.v; if(this->v>=umod)this->v-=umod; return *this; } inline mint &operator-=(const mint &b){ this->v-=b.v; if(this->v>=umod)this->v+=umod; return *this; } inline mint &operator*=(const mint &b){ this->v=((unsigned long long)this->v*b.v)%umod; return *this; } inline mint &operator/=(const mint &b){ *this*=b.inv(); return *this; } inline mint operator+()const{return *this;} inline mint operator-()const{return mint()-*this;} friend inline mint operator+(const mint &a,const mint &b){return mint(a)+=b;} friend inline mint operator-(const mint &a,const mint &b){return mint(a)-=b;} friend inline mint operator*(const mint &a,const mint &b){return mint(a)*=b;} friend inline mint operator/(const mint &a,const mint &b){return mint(a)/=b;} friend inline bool operator==(const mint &a,const mint &b){return a.val()==b.val();} friend inline bool operator!=(const mint &a,const mint &b){return !(a==b);} inline mint operator++(int){ mint ret=*this; *this+=1; return ret; } inline mint operator--(int){ mint ret=*this; *this-=1; return ret; } mint pow(long long n)const{ mint ret=mint::raw(1),a(*this); while(n){ if(n&1)ret*=a; a*=a; n>>=1; } return ret; } inline mint inv()const{ assert(this->v!=0); return pow(m-2); } mint sqrt()const{ if(this->val()<=1)return *this; if constexpr(m%8==1){ mint b=2; while(b.pow((m-1)/2).val()==1)b++; int m2=m-1,e=0; while(m2%2==0)m2>>=1,e++; mint x=this->pow((m2-1)/2); mint y=(*this)*x*x; x*=*this; mint z=b.pow(m2); while(y.val()!=1){ int j=0; mint t=y; while(t.val()!=1)t*=t,j++; z=z.pow(1<<(e-j-1)); x*=z; z*=z; y*=z;e=j; } return x; } else if constexpr(m%8==5){ mint ret=this->pow((m+3)/8); if((ret*ret).val()==this->val())return ret; else return ret*mint::raw(2).pow((m-1)/4); } else{ return this->pow((m+1)/4); } } std::pair safe_sqrt()const{ if(this->pow((m-1)/2)==1)return std::make_pair(true,this->sqrt()); else return std::make_pair(false,0); } friend std::istream &operator>>(std::istream &is,mint &b){ long long a; is>>a; b=mint(a); return is; } friend std::ostream &operator<<(std::ostream &os,const mint &b){ os<; using mint107=mod_int<1000000007>; int sqrt_mod(int n,int mod){ if(n<=1)return n; auto powmod=[&](int a,int b)->int { ll ret=1; while(b){ if(b&1)ret=(ret*a)%mod; a=ll(a)*a%mod; b>>=1; } return ret; }; if(powmod(n,(mod-1)/2)!=1)return -1; if(mod%8==1){ int b=2; while(powmod(b,(mod-1)/2)==1)b++; int m2=mod-1,e=0; while(m2%2==0)m2>>=1,e++; int x=powmod(n,(m2-1)/2); int y=(ll(n)*x%mod)*x%mod; x=ll(x)*n%mod; int z=powmod(b,m2); while(y!=1){ int j=0; int t=y; while(t!=1)t=ll(t)*t%mod,j++; z=powmod(z,1<<(e-j-1)); x=ll(x)*z%mod; z=ll(z)*z%mod; y=ll(y)*z%mod; e=j; } return x; } else if(mod%8==5){ int ret=powmod(n,(mod+3)/8); if(ll(ret)*ret%mod==n)return ret; else{ ret=(ll(ret)*powmod(2,(mod-1)/4))%mod; return ret; } } else return powmod(n,(mod+1)/4); } template vector>convolution2d(const vector>&a,const vector>&b){ int x=a.size()+b.size()-1,y=a[0].size()+b[0].size()-1; vectorf(a.size()*y),g(b.size()*y); rep(i,a.size())rep(j,a[i].size())f[i*y+j]=a[i][j]; rep(i,b.size())rep(j,b[i].size())g[i*y+j]=b[i][j]; auto c=ntt_convolution(f,g); vector>ret(x,vector(y)); rep(i,x)rep(j,y)ret[i][j]=c[i*y+j]; return ret; } template vector fps_inv(const vector &a,int deg=-1){ int n=a.size(); if(deg==-1)deg=n; assert(a[0]!=T(0)); vector ret(deg); ret[0]=a[0].inv(); for(int m=1;m f(a.begin(),a.begin()+min(n,m*2)); if(f.size() g(ret); f.resize(m*2); dft(f); g.resize(m*2); dft(g); rep(i,m*2)f[i]*=g[i]; idft(f); T inv=T::raw(m*2).inv(); rep(i,m)f[i]=0; reps(i,m,m*2)f[i]*=inv; dft(f); rep(i,m*2)f[i]*=g[i]; idft(f); rep(i,m*2)f[i]*=inv; reps(i,m,min(deg,m*2))ret[i]-=f[i]; } return ret; } template vector fps_diff(const vector&a){ int n=a.size(); vectorb(max(0,n-1)); reps(i,1,n)b[i-1]=a[i]*i; return b; } template vector fps_integral(const vector&a){ int n=a.size(); vectorb(n+1); b[0]=0; if(n)b[1]=1; reps(i,2,n+1)b[i]=-b[T::mod()%i]*(T::mod()/i); rep(i,n)b[i+1]*=a[i]; return b; } template vector fps_log(const vector&a,int deg=-1){ int n=a.size(); if(deg==-1)deg=n; vectorb=fps_integral(ntt_convolution(fps_diff(a),fps_inv(a,deg))); return {b.begin(),b.begin()+deg}; } template vector fps_exp(const vector&a,int deg=-1){ assert(a.empty()||a[0]==0); int n=a.size(); if(deg==-1)deg=n; vectorb{1}; for(int m=1;mnxt=fps_log(b,m*2); rep(i,m*2)nxt[i]*=-1; rep(i,min(n,m*2))nxt[i]+=a[i]; nxt[0]++; b=ntt_convolution(b,nxt); b.resize(m*2); } return {b.begin(),b.begin()+deg}; } template vector fps_pow(vector a,ll k,int deg=-1){ int n=a.size(); if(deg==-1)deg=n; if(k==0){ vectorret(deg,0); ret[0]=1; return ret; } int of=0; while(a[of]==0&&of!=n)of++; if(of==n)return vector(deg,0); if(of!=0&&k>=deg)return vector(deg,0); if(of*k>=deg)return vector(deg,0); a.erase(a.begin(),a.begin()+of); n=a.size(); T a0=a[0]; T inv=a[0].inv(); rep(i,n)a[i]*=inv; vectorlg=fps_log(a,deg); rep(i,deg)lg[i]*=k; vectorep=fps_exp(lg,deg); T pw=a0.pow(k); rep(i,deg)ep[i]*=pw; vectorret(deg,0); reps(i,of*k,deg)ret[i]=ep[i-of*k]; return ret; } template vectorfps_pows(vectorf,vectorg,int n){ int sz=f.size(); vector>p(sz,vector(2)),q(sz,vector(2)); q[0][0]=1; rep(i,min(g.size(),sz))p[i][0]=g[i]; rep(i,sz)q[i][1]=-f[i]; int k=sz-1; while(k){ int x=p.size(),y=p[0].size(); vector>mq(q); for(int i=1;i>f=convolution2d(p,mq); vector>g=convolution2d(q,mq); p.resize(k/2+1); q.resize(k/2+1); if(k&1)rep(i,p.size())p[i]=f[i*2+1]; else rep(i,p.size())p[i]=f[i*2]; rep(i,q.size())q[i]=g[i*2]; rep(i,p.size())if(p[i].size()>n+1)p[i].resize(n+1); rep(i,q.size())if(q[i].size()>n+1)q[i].resize(n+1); k>>=1; } vectorf2(p[0]),g2(q[0]); vectorret=ntt_convolution(f2,fps_inv(g2)); ret.resize(n+1); return ret; } template vectorfps_divmod(vectorf,vectorg){ if(f.empty())return f; int n=f.size(),m=g.size(); vectorr(f); reverse(all(f)),reverse(all(g)); vectorq=ntt_convolution(f,fps_inv(g,n)); q.resize(max(0,n-m+1)); auto p=ntt_convolution(g,q); reverse(all(p)); rep(i,min(p.size(),m))r[i]-=p[i]; r.resize(m-1); while(!r.empty()&&r.back().val()==0)r.pop_back(); return r; } template vectorfps_sqrt(vectorf,int deg=-1){ if(deg==-1)deg=f.size(); int prefix_zero=0; while(prefix_zero(deg,0); if(prefix_zero&1)return vector{}; f.erase(f.begin(),f.begin()+prefix_zero); prefix_zero/=2; int sq=sqrt_mod(f[0].val(),T::mod()); if(sq==-1)return vector{}; T inv0=f[0].inv(); rep(i,f.size())f[i]*=inv0; vectorg{1}; debug(f); T inv2=T::raw(2).inv(); while(g.size()fp(f.begin(),f.begin()+min(f.size(),g.size()*2)); fp=ntt_convolution(fp,fps_inv(g,g.size()*2)); vectornxtg(g); nxtg.resize(g.size()*2); rep(i,nxtg.size()){ if(i std::vectorfps_middle_product(std::vectora,std::vectorb){ assert(a.size()>=b.size()); std::reverse(b.begin(),b.end()); int n=a.size(),m=b.size(); int z=ceil_pow2(n); a.resize(z,0),b.resize(z,0); dft(a),dft(b); for(int i=0;i(a.begin()+m-1,a.begin()+n); } template std::vectormultipoint_evaluation(std::vectorf,const std::vector&x){ int n=ceil_pow2((int)x.size()),log2n=msb(n); std::vector>f2(log2n+1,std::vector(n*2)); for(int i=0;il(f2[i].begin()+j,f2[i].begin()+j+b); std::vectorr(f2[i].begin()+j+b*2,f2[i].begin()+j+b*3); l.resize(b*2,0),r.resize(b*2,0); ntt_doubling(l),ntt_doubling(r); for(int k=0;kg(f2[log2n].begin(),f2[log2n].begin()+n); idft(g); { T inv=T::raw(g.size()).inv(); for(int i=0;i=0;i--){ std::vectornxtf(n); int b=1<l(b*2),r(b*2); for(int k=0;k>n; vectora(n); cin>>a; mint ans=1; auto dfs=[&](auto self,int l,int r)->vector { if(l+1==r)return {a[l],-1}; int mid=(l+r)/2; vectorpl=self(self,l,mid); vectorpr=self(self,mid,r); vectorx(a.begin()+mid,a.begin()+r); vectorb=multipoint_evaluation(pl,x); for(auto i:b)ans*=i; return ntt_convolution(pl,pr); }; dfs(dfs,0,n); cout<