結果

問題 No.2513 Power Eraser
コンテスト
ユーザー Taiki0715
提出日時 2026-08-26 16:20:44
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 365 ms / 6,000 ms
+ 587µs
コード長 47,260 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,797 ms
コンパイル使用メモリ 340,884 KB
実行使用メモリ 46,396 KB
最終ジャッジ日時 2026-08-26 16:21:05
合計ジャッジ時間 18,181 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#define PROBLEM "https://yukicoder.me/problems/no/2513"
#define NTT_SIMD
#include<vector>
#include<immintrin.h>
#include<cstdint>
#include<array>
#include<numeric>
#include<initializer_list>
constexpr bool isprime_constexpr(unsigned long long n){
  if(n==998244353)return true;
  if(n==1000000007)return true;
  if(n<64)return 2891462833508853932ll>>n&1;
  if(n%2==0)return false;
  unsigned long long d=n-1;
  int s=0;
  while(!(d&1))d>>=1,s++;
  int q=63;
  while(!(d>>q))q--;
  unsigned long long r=n;
  for(int i=0;i<5;i++)r*=2-r*n;
  auto redc=[&r,&n](__uint128_t x)->unsigned long long {
    x=(x+__uint128_t((unsigned long long)x*-r)*n)>>64;
    return x>=n?x-n:x;
  };
  __uint128_t r2=-__uint128_t(n)%n;
  unsigned long long one=redc(r2);
  for(unsigned long long base:{2,325,9375,28178,450775,9780504,1795265022}){
    if(base%n==0)continue;
    unsigned long long a=base=redc((base%n)*r2);
    for(int i=q-1;i>=0;i--){
      a=redc(__uint128_t(a)*a);
      if(d>>i&1)a=redc(__uint128_t(a)*base);
    }
    if(a==one)continue;
    for(int i=1;a!=n-one;i++){
      if(i>=s)return false;
      a=redc(__uint128_t(a)*a);
    }
  }
  return true;
}
constexpr int factorize_constexpr(unsigned long long n,unsigned long long*a){
  if(n<=1)return 0;
  int ptr=0;
  while(n%2==0){
    a[ptr++]=2;
    n/=2;
  }
  while(n>1){
    if(isprime_constexpr(n)){
      a[ptr++]=n;
      break;
    }
    unsigned long long pf=n;
    unsigned long long x=0,y=0;
    int c=1;
    do{
      x=((__uint128_t)x*x+c)%pf;
      y=((__uint128_t)y*y+c)%pf;
      y=((__uint128_t)y*y+c)%pf;
      unsigned long long g=std::gcd(x>y?x-y:y-x,pf);
      if(g==pf){
        c++;
        x=y=0;
        continue;
      }
      if(g>1)pf=g;
    }while(!isprime_constexpr(pf));
    while(n%pf==0){
      a[ptr++]=pf;
      n/=pf;
    }
  }
  return ptr;
}
#include<type_traits>
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits<=32),T>pow_mod(T a,T n,T mod){
  using u64=unsigned long long;
  u64 res=1;
  while(n>0){
    if(n&1)res=((u64)res*a)%mod;
    a=((u64)a*a)%mod;
    n>>=1;
  }
  return T(res);
}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),T>pow_mod(T a,T n,T mod){
  using u128=__uint128_t;
  u128 res=1;
  while(n>0){
    if(n&1)res=((u128)res*a)%mod;
    a=((u128)a*a)%mod;
    n>>=1;
  }
  return T(res);
}
namespace Random{
constexpr unsigned long long to_seed(const char*s){
  unsigned long long h=14695981039346656037ULL;
  while(*s){
    h^=static_cast<unsigned char>(*s++);
    h*=1099511628211ULL;
  }
  return h;
}
constexpr unsigned long long constexpr_random_seed=(to_seed(__TIME__)*0x9e3779b97f4a7c15ULL)^to_seed(__DATE__);
constexpr unsigned long long next_value(unsigned long long n){
  n^=n<<13;
  n^=n>>7;
  n^=n<<17;
  return n;
}
}
constexpr unsigned long long primitive_root_constexpr(unsigned long long x){
  if(!isprime_constexpr(x))throw "not prime";
  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;
  unsigned long long a[64]={};
  int ptr=factorize_constexpr(x-1,a);
  for(unsigned long long v=Random::constexpr_random_seed;;){
    unsigned long long g=v%(x-1)+1;
    bool ok=true;
    for(int i=0;i<ptr;i++){
      if(i>0&&a[i-1]==a[i])continue;
      if(pow_mod<unsigned long long>(g,(x-1)/a[i],x)==1){
        ok=false;
        break;
      }
    }
    if(ok)return g;
    v=Random::next_value(v);
  }
}
#include<limits>
#include<concepts>
template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>msb(T n){return n==0?-1:31-__builtin_clz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>msb(T n){return n==0?-1:63-__builtin_clzll(n);}

template<typename T>
constexpr std::enable_if_t<std::numeric_limits<T>::digits<=32,int>lsb(T n){return n==0?-1:__builtin_ctz(n);}
template<typename T>
constexpr std::enable_if_t<(std::numeric_limits<T>::digits>32),int>lsb(T n){return n==0?-1:__builtin_ctzll(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>floor_pow2(T n){return n==0?0:T(1)<<msb(n);}

template<typename T>
constexpr std::enable_if_t<std::is_integral_v<T>,T>ceil_pow2(T n){return n<=1?1:T(1)<<(msb(n-1)+1);}

template<std::integral T>
constexpr T safe_div(T a,T b){return a/b-(a%b&&(a^b)<0);}
template<std::integral T>
constexpr T safe_ceil(T a,T b){return a/b+(a%b&&(a^b)>0);}
template<auto m>
struct ntt_root{
  static_assert(1<=m&&m<(1ull<<63));
  static_assert(isprime_constexpr(m));
  using value_type=std::conditional_t<((m>>31)==0),uint32_t,uint64_t>;
  using mul_type=std::conditional_t<((m>>31)==0),uint64_t,__uint128_t>;
  static constexpr int rank2=lsb(m-1);
  static constexpr value_type g=primitive_root_constexpr(m);
  std::array<value_type,rank2+1>root,invroot;
  std::array<value_type,std::max(0,rank2-1)>rate2,invrate2;
  std::array<value_type,std::max(0,rank2-2)>rate3,invrate3;
  constexpr ntt_root(){
    root[rank2]=pow_mod<value_type>(g,m>>rank2,m);
    invroot[rank2]=pow_mod<value_type>(root[rank2],m-2,m);
    for(int i=rank2-1;i>=0;i--){
      root[i]=(mul_type)root[i+1]*root[i+1]%m;
      invroot[i]=(mul_type)invroot[i+1]*invroot[i+1]%m;
    }
    value_type prod=1,invprod=1;
    for(int i=0;i<rank2-1;i++){
      rate2[i]=(mul_type)root[i+2]*prod%m;
      invrate2[i]=(mul_type)invroot[i+2]*invprod%m;
      prod=(mul_type)prod*invroot[i+2]%m;
      invprod=(mul_type)invprod*root[i+2]%m;
    }
    prod=invprod=1;
    for(int i=0;i<rank2-2;i++){
      rate3[i]=(mul_type)root[i+3]*prod%m;
      invrate3[i]=(mul_type)invroot[i+3]*invprod%m;
      prod=(mul_type)prod*invroot[i+3]%m;
      invprod=(mul_type)invprod*root[i+3]%m;
    }
  }
};
template<unsigned m>
struct Montgomery{
  static_assert(m%2==1);
  static_assert(m>=3);
  static_assert(m<(1u<<30));
  static constexpr unsigned r=[](){
    unsigned res=m;
    for(int i=0;i<4;i++)res*=2-m*res;
    return res;
  }();
  static constexpr unsigned r2=-((unsigned long long)m)%m;
  static constexpr unsigned reduce(unsigned long long x){
    return (x+(unsigned long long)((unsigned)x*(unsigned)(-r))*m)>>32;
  }
  static constexpr unsigned mod(){return m;}
};
template<typename MM>
__attribute__((target("avx2")))
inline __m256i add_simd(__m256i x,__m256i y){
  x=_mm256_add_epi32(x,y);
  x=_mm256_sub_epi32(x,_mm256_set1_epi32(MM::mod()*2));
  __m256i mask=_mm256_cmpgt_epi32(_mm256_set1_epi32(0),x);
  x=_mm256_add_epi32(x,_mm256_and_si256(_mm256_set1_epi32(MM::mod()*2),mask));
  return x;
}
template<typename MM>
__attribute__((target("avx2")))
inline __m256i sub_simd(__m256i x,__m256i y){
  x=_mm256_sub_epi32(x,y);
  __m256i mask=_mm256_cmpgt_epi32(_mm256_set1_epi32(0),x);
  x=_mm256_add_epi32(x,_mm256_and_si256(_mm256_set1_epi32(MM::mod()*2),mask));
  return x;
}
template<typename MM>
__attribute__((target("avx2")))
inline void mul_simd(__m256i&x,int y){
  __m256i x0246=_mm256_mul_epu32(x,_mm256_set1_epi32(y));
  __m256i x0246_2=_mm256_mul_epu32(x0246,_mm256_set1_epi32(-MM::r));
  x0246_2=_mm256_mul_epu32(x0246_2,_mm256_set1_epi32(MM::mod()));
  x0246=_mm256_add_epi64(x0246,x0246_2);
  __m256i x1357=_mm256_mul_epu32(_mm256_srli_epi64(x,32),_mm256_set1_epi32(y));
  __m256i x1357_2=_mm256_mul_epu32(x1357,_mm256_set1_epi32(-MM::r));
  x1357_2=_mm256_mul_epu32(x1357_2,_mm256_set1_epi32(MM::mod()));
  x1357=_mm256_add_epi64(x1357,x1357_2);
  x=_mm256_blend_epi32(_mm256_srli_epi64(x0246,32),x1357,0b10101010);
}
template<typename MM>
__attribute__((target("avx2")))
inline void mul_simd(__m256i&x,const __m256i&y){
  __m256i x0246=_mm256_mul_epu32(x,y);
  __m256i x0246_2=_mm256_mul_epu32(x0246,_mm256_set1_epi32(-MM::r));
  x0246_2=_mm256_mul_epu32(x0246_2,_mm256_set1_epi32(MM::mod()));
  x0246=_mm256_add_epi64(x0246,x0246_2);
  __m256i x1357=_mm256_mul_epu32(_mm256_srli_epi64(x,32),_mm256_srli_epi64(y,32));
  __m256i x1357_2=_mm256_mul_epu32(x1357,_mm256_set1_epi32(-MM::r));
  x1357_2=_mm256_mul_epu32(x1357_2,_mm256_set1_epi32(MM::mod()));
  x1357=_mm256_add_epi64(x1357,x1357_2);
  x=_mm256_blend_epi32(_mm256_srli_epi64(x0246,32),x1357,0b10101010);
}
namespace ntt_simd_impl{
alignas(32) unsigned b[1<<24];
template<typename T>
__attribute__((target("avx2")))
void dft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.rate2.size()>rate2_m=[](){
    std::array<unsigned,r.rate2.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate2[i]*MM::r2);
    return res;
  }();
  static constexpr std::array<unsigned,r.rate3.size()>rate3_m=[](){
    std::array<unsigned,r.rate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>rate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.root[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.invroot[i+4]%MM::mod();
    }
    return res;
  }();
  int h=lsb(a.size());
  int len=0;
  for(int i=0;i<(int)a.size();i++)b[i]=a[i].val();
  for(int i=0;i<(int)a.size();i+=8){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
    mul_simd<MM>(u,MM::r2);
    _mm256_storeu_si256((__m256i*)(b+i),u);
  }
  while(len+4<h){
    int p=1<<(h-len-2);
    unsigned rot=rate3_m[0],imag=MM::reduce((unsigned long long)r.root[2]*MM::r2);
    {
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+i+p));
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+i+p*2));
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+i+p*3));
        __m256i m=sub_simd<MM>(b1,b3);
        mul_simd<MM>(m,imag);
        __m256i b02=add_simd<MM>(b0,b2);
        __m256i b0n2=sub_simd<MM>(b0,b2);
        __m256i b13=add_simd<MM>(b1,b3);
        _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+i+p),sub_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+i+p*2),add_simd<MM>(b0n2,m));
        _mm256_storeu_si256((__m256i*)(b+i+p*3),sub_simd<MM>(b0n2,m));
      }
    }
    for(int s=1;s<(1<<len);s++){
      unsigned rot2=MM::reduce((unsigned long long)rot*rot);
      unsigned rot3=MM::reduce((unsigned long long)rot2*rot);
      int offset=s<<(h-len);
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+i+p));
        mul_simd<MM>(b1,rot);
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+offset+i+p*2));
        mul_simd<MM>(b2,rot2);
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+offset+i+p*3));
        mul_simd<MM>(b3,rot3);
        __m256i m=sub_simd<MM>(b1,b3);
        mul_simd<MM>(m,imag);
        __m256i b02=add_simd<MM>(b0,b2);
        __m256i b0n2=sub_simd<MM>(b0,b2);
        __m256i b13=add_simd<MM>(b1,b3);
        _mm256_storeu_si256((__m256i*)(b+offset+i),add_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+offset+i+p),sub_simd<MM>(b02,b13));
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*2),add_simd<MM>(b0n2,m));
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*3),sub_simd<MM>(b0n2,m));
      }
      rot=MM::reduce((unsigned long long)rot*rate3_m[lsb(~(unsigned)s)]);
    }
    len+=2;
  }
  if(len+4==h){
    unsigned rot=MM::reduce(MM::r2);
    for(int s=0;s<(1<<len);s++){
      int offset=s<<(h-len);
      __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset));
      __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+8));
      mul_simd<MM>(b1,rot);
      _mm256_storeu_si256((__m256i*)(b+offset),add_simd<MM>(b0,b1));
      _mm256_storeu_si256((__m256i*)(b+offset+8),sub_simd<MM>(b0,b1));
      rot=MM::reduce((unsigned long long)rot*rate2_m[lsb(~(unsigned)s)]);
    }
  }
  unsigned one=MM::reduce(MM::r2);
  unsigned r2=MM::reduce((unsigned long long)r.root[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.root[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,rot);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    for(int i=0;i<8;i++){
      if(b[s*8+i]>=T::mod())b[s*8+i]-=T::mod();
      a[s*8+i]=T::raw(b[s*8+i]);
    }
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(rate4_m.data()+8*lsb(~(unsigned)s))));
  }
}
template<typename T>
__attribute__((target("avx2")))
void idft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.invrate3.size()>invrate3_m=[](){
    std::array<unsigned,r.invrate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.invrate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>invrate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.invroot[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.root[i+4]%MM::mod();
    }
    return res;
  }();
  int h=lsb(a.size());
  int len=h;
  for(int i=0;i<(int)a.size();i++)b[i]=a[i].val();
  unsigned one=MM::reduce(MM::r2);
  unsigned r2=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.invroot[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,MM::r2);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,rot);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(invrate4_m.data()+8*lsb(~(unsigned)s))));
  }
  len-=3;
  while(len>=2){
    int p=1<<(h-len);
    unsigned rot=invrate3_m[0],imag=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
    {
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+i+p));
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+i+p*2));
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+i+p*3));
        __m256i b01=add_simd<MM>(b0,b1);
        __m256i b0n1=sub_simd<MM>(b0,b1);
        __m256i b23=add_simd<MM>(b2,b3);
        __m256i k=sub_simd<MM>(b2,b3);
        mul_simd<MM>(k,imag);
        _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(b01,b23));
        b1=add_simd<MM>(b0n1,k);
        _mm256_storeu_si256((__m256i*)(b+i+p),b1);
        b2=sub_simd<MM>(b01,b23);
        _mm256_storeu_si256((__m256i*)(b+i+p*2),b2);
        b3=sub_simd<MM>(b0n1,k);
        _mm256_storeu_si256((__m256i*)(b+i+p*3),b3);
      }
    }
    for(int s=1;s<(1<<(len-2));s++){
      int offset=s<<(h-len+2);
      unsigned rot2=MM::reduce((unsigned long long)rot*rot);
      unsigned rot3=MM::reduce((unsigned long long)rot2*rot);
      for(int i=0;i<p;i+=8){
        __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset+i));
        __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+i+p));
        __m256i b2=_mm256_loadu_si256((__m256i*)(b+offset+i+p*2));
        __m256i b3=_mm256_loadu_si256((__m256i*)(b+offset+i+p*3));
        __m256i b01=add_simd<MM>(b0,b1);
        __m256i b0n1=sub_simd<MM>(b0,b1);
        __m256i b23=add_simd<MM>(b2,b3);
        __m256i k=sub_simd<MM>(b2,b3);
        mul_simd<MM>(k,imag);
        _mm256_storeu_si256((__m256i*)(b+offset+i),add_simd<MM>(b01,b23));
        b1=add_simd<MM>(b0n1,k);
        mul_simd<MM>(b1,rot);
        _mm256_storeu_si256((__m256i*)(b+offset+i+p),b1);
        b2=sub_simd<MM>(b01,b23);
        mul_simd<MM>(b2,rot2);
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*2),b2);
        b3=sub_simd<MM>(b0n1,k);
        mul_simd<MM>(b3,rot3);
        _mm256_storeu_si256((__m256i*)(b+offset+i+p*3),b3);
      }
      rot=MM::reduce((unsigned long long)rot*invrate3_m[lsb(~(unsigned)s)]);
    }
    len-=2;
  }
  if(len==1){
    for(int i=0;i<(1<<(h-1));i+=8){
      __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
      __m256i v=_mm256_loadu_si256((__m256i*)(b+(1<<(h-1))+i));
      _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(u,v));
      _mm256_storeu_si256((__m256i*)(b+(1<<(h-1))+i),sub_simd<MM>(u,v));
    }
  }
  for(int i=0;i<(int)a.size();i+=8){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+i),u);
    for(int j=0;j<8;j++){
      if(b[i+j]>=T::mod())b[i+j]-=T::mod();
      a[i+j]=T::raw(b[i+j]);
    }
  }
}
template<typename T>
__attribute__((target("avx2")))
void transposed_dft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.rate2.size()>rate2_m=[](){
    std::array<unsigned,r.rate2.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate2[i]*MM::r2);
    return res;
  }();
  static constexpr std::array<unsigned,r.rate3.size()>rate3_m=[](){
    std::array<unsigned,r.rate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.rate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>rate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.root[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.invroot[i+4]%MM::mod();
    }
    return res;
  }();
  for(int i=0;i<(int)a.size();i++)b[i]=a[i].val();
  int h=lsb(a.size());
  int len=h;
  unsigned one=MM::reduce(MM::r2);
  unsigned r2=MM::reduce((unsigned long long)r.root[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.root[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,MM::r2);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,rot);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(rate4_m.data()+8*lsb(~(unsigned)s))));
  }
  len-=3;
  if(len%2!=0){
    unsigned rot=one;
    for(int s=0;s<(1<<(len-1));s++){
      int offset=s<<(h-len+1);
      __m256i b0=_mm256_loadu_si256((__m256i*)(b+offset));
      __m256i b1=_mm256_loadu_si256((__m256i*)(b+offset+8));
      _mm256_storeu_si256((__m256i*)(b+offset),add_simd<MM>(b0,b1));
      b0=sub_simd<MM>(b0,b1);
      mul_simd<MM>(b0,rot);
      _mm256_storeu_si256((__m256i*)(b+offset+8),b0);
      rot=MM::reduce((unsigned long long)rot*rate2_m[lsb(~(unsigned)s)]);
    }
    len--;
  }
  while(len>0){
    int p=1<<(h-len);
    unsigned rot=one,imag=MM::reduce((unsigned long long)r.root[2]*MM::r2);
    for(int s=0;s<(1<<(len-2));s++){
      const unsigned rot1=rot,rot2=MM::reduce((unsigned long long)rot*rot),rot3=MM::reduce((unsigned long long)rot*rot2);
      int offset=s<<(h-len+2);
      for(int i=0;i<p;i+=8){
        __m256i a0=_mm256_loadu_si256((__m256i*)(b+i+offset));
        __m256i a1=_mm256_loadu_si256((__m256i*)(b+i+offset+p));
        __m256i a2=_mm256_loadu_si256((__m256i*)(b+i+offset+p*2));
        __m256i a3=_mm256_loadu_si256((__m256i*)(b+i+offset+p*3));
        __m256i k=sub_simd<MM>(a2,a3);
        mul_simd<MM>(k,imag);
        __m256i a01=add_simd<MM>(a0,a1),a23=add_simd<MM>(a2,a3);
        _mm256_storeu_si256((__m256i*)(b+i+offset),add_simd<MM>(a01,a23));
        a2=sub_simd<MM>(a01,a23);
        mul_simd<MM>(a2,rot2);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*2),a2);
        a01=sub_simd<MM>(a0,a1);
        a1=add_simd<MM>(a01,k);
        a3=sub_simd<MM>(a01,k);
        mul_simd<MM>(a1,rot1);
        mul_simd<MM>(a3,rot3);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p),a1);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*3),a3);
      }
      if(s+1!=1<<(len-2))rot=MM::reduce((unsigned long long)rot*rate3_m[lsb(~(unsigned)s)]);
    }
    len-=2;
  }
  for(int i=0;i<(int)a.size();i+=8){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+i),u);
    for(int j=0;j<8;j++){
      if(b[i+j]>=T::mod())b[i+j]-=T::mod();
      a[i+j]=T::raw(b[i+j]);
    }
  }
}
template<typename T>
__attribute__((target("avx2")))
void transposed_idft_simd(std::vector<T>&a){
  using MM=Montgomery<T::mod()>;
  static constexpr ntt_root<T::mod()>r;
  static constexpr std::array<unsigned,r.invrate3.size()>invrate3_m=[](){
    std::array<unsigned,r.invrate3.size()>res;
    for(int i=0;i<(int)res.size();i++)res[i]=MM::reduce((unsigned long long)r.invrate3[i]*MM::r2);
    return res;
  }();
  alignas(32) static constexpr std::array<unsigned,(r.rank2-3)*8>invrate4_m=[](){
    std::array<unsigned,(r.rank2-3)*8>res;
    unsigned prod=1;
    for(int i=0;i<=r.rank2-4;i++){
      unsigned v=(unsigned long long)r.invroot[i+4]*prod%MM::mod();
      v=MM::reduce((unsigned long long)v*MM::r2);
      res[i*8]=MM::reduce(MM::r2);
      for(int j=1;j<8;j++)res[i*8+j]=MM::reduce((unsigned long long)res[i*8+j-1]*v);
      prod=(unsigned long long)prod*r.root[i+4]%MM::mod();
    }
    return res;
  }();
  unsigned one=MM::reduce(MM::r2);
  for(int i=0;i<(int)a.size();i++)b[i]=MM::reduce((unsigned long long)a[i].val()*MM::r2);
  int h=lsb(a.size());
  int len=0;
  if(h%2==0){
    for(int i=0;i<(1<<(h-1));i+=8){
      __m256i u=_mm256_loadu_si256((__m256i*)(b+i));
      __m256i v=_mm256_loadu_si256((__m256i*)(b+(1<<(h-1))+i));
      _mm256_storeu_si256((__m256i*)(b+i),add_simd<MM>(u,v));
      _mm256_storeu_si256((__m256i*)(b+(1<<(h-1))+i),sub_simd<MM>(u,v));
    }
    len++;
  }
  while(len+3<h){
    int p=1<<(h-len-2);
    unsigned rot=one,imag=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
    for(int s=0;s<(1<<len);s++){
      const unsigned rot1=rot,rot2=MM::reduce((unsigned long long)rot*rot),rot3=MM::reduce((unsigned long long)rot*rot2);
      int offset=s<<(h-len);
      for(int i=0;i<p;i+=8){
        __m256i a0=_mm256_loadu_si256((__m256i*)(b+i+offset));
        __m256i a1=_mm256_loadu_si256((__m256i*)(b+i+offset+p));
        mul_simd<MM>(a1,rot1);
        __m256i a2=_mm256_loadu_si256((__m256i*)(b+i+offset+p*2));
        mul_simd<MM>(a2,rot2);
        __m256i a3=_mm256_loadu_si256((__m256i*)(b+i+offset+p*3));
        mul_simd<MM>(a3,rot3);
        __m256i k=sub_simd<MM>(a1,a3);
        mul_simd<MM>(k,imag);
        __m256i a02=add_simd<MM>(a0,a2),a13=add_simd<MM>(a1,a3);
        _mm256_storeu_si256((__m256i*)(b+i+offset),add_simd<MM>(a02,a13));
        _mm256_storeu_si256((__m256i*)(b+i+offset+p),sub_simd<MM>(a02,a13));
        a02=sub_simd<MM>(a0,a2);
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*2),add_simd<MM>(a02,k));
        _mm256_storeu_si256((__m256i*)(b+i+offset+p*3),sub_simd<MM>(a02,k));
      }
      if(s+1!=1<<len)rot=MM::reduce((unsigned long long)rot*invrate3_m[lsb(~(unsigned)s)]);
    }
    len+=2;
  }
  unsigned r2=MM::reduce((unsigned long long)r.invroot[2]*MM::r2);
  unsigned r3=MM::reduce((unsigned long long)r.invroot[3]*MM::r2);
  unsigned r3_2=MM::reduce((unsigned long long)r3*r3);
  unsigned r3_3=MM::reduce((unsigned long long)r3_2*r3);
  __m256i p1=_mm256_set_epi32(r3_3,r3_2,r3,one,one,one,one,one);
  __m256i p2=_mm256_set_epi32(r2,one,one,one,r2,one,one,one);
  __m256i rot=_mm256_set1_epi32(one);
  for(int s=0;s<(int)a.size()/8;s++){
    __m256i u=_mm256_loadu_si256((__m256i*)(b+s*8));
    mul_simd<MM>(u,rot);
    __m256i v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11110000);
    v=_mm256_permute2x128_si256(v,v,0b01);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p1);
      v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b11001100);
    v=_mm256_shuffle_epi32(v,0b01001110);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,p2);
    v=u;
    u=_mm256_blend_epi32(u,sub_simd<MM>(_mm256_setzero_si256(),u),0b10101010);
    v=_mm256_shuffle_epi32(v,0b10110001);
    u=add_simd<MM>(u,v);
    mul_simd<MM>(u,1);
    _mm256_storeu_si256((__m256i*)(b+s*8),u);
    for(int j=0;j<8;j++){
      if(b[s*8+j]>=T::mod())b[s*8+j]-=T::mod();
      a[s*8+j]=T::raw(b[s*8+j]);
    }
    mul_simd<MM>(rot,_mm256_loadu_si256((__m256i*)(invrate4_m.data()+8*lsb(~(unsigned)s))));
  }
}
}
using ntt_simd_impl::dft_simd;
using ntt_simd_impl::idft_simd;
using ntt_simd_impl::transposed_dft_simd;
using ntt_simd_impl::transposed_idft_simd;
#include<stdio.h>
#include<string.h>
#include<string>
#include<cstdlib>
namespace fastio{
static constexpr uint32_t buf_size=1<<17;
char ibuf[buf_size];
char obuf[buf_size];
uint32_t pil=0,pir=0,por=0;
struct Pre{
  char t[40000];
  constexpr Pre(){
    for(int i=0;i<10000;i++){
      int n=i;
      for(int j=3;j>=0;j--){
        t[i*4+j]='0'+n%10;
        n/=10;
      }
    }
  }
}constexpr pre;
inline void load(){
  if(pir-pil<=pil)memcpy(ibuf,ibuf+pil,pir-pil);
  else memmove(ibuf,ibuf+pil,pir-pil);
  pir=pir-pil+fread(ibuf+pir-pil,1,buf_size-pir+pil,stdin);
  pil=0;
}
inline void flush(){
  fwrite(obuf,1,por,stdout);
  por=0;
}
inline void rd(char&c){c=ibuf[pil++];}
inline void rd(std::string&s){
  char c;
  s.clear();
  if(pil==pir)load();
  rd(c);
  if(pil==pir)load();
  while(!std::isspace(c)){
    s+=c;
    rd(c);
    if(pil==pir)load();
  }
}
inline void rd(__int128_t&x){
  if(pil+50>pir)load();
  char c;
  do rd(c); while(c<'-');
  bool neg=false;
  if(c=='-'){
    neg=true;
    rd(c);
  }
  x=0;
  while(c>='0'){
    x=x*10+(c&15);
    rd(c);
  }
  if(neg)x=-x;
}
template<typename T>
inline void rd(T&x){
  if(pil+32>pir)load();
  char c;
  do rd(c); while(c<'-');
  bool neg=false;
  if constexpr(std::is_signed_v<T>){
    if(c=='-'){
      neg=true;
      rd(c);
    }
  }
  x=0;
  while(c>='0'){
    x=x*10+(c&15);
    rd(c);
  }
  if constexpr(std::is_signed_v<T>){
    if(neg)x=-x;
  }
}
inline void wt(char x){obuf[por++]=x;}
template<typename T,std::enable_if_t<std::is_integral_v<T>,std::nullptr_t> =nullptr>
inline void wt(T x){
  if(por+32>buf_size)flush();
  if(x==0){
    wt('0');
    return;
  }
  if constexpr(std::is_signed_v<T>){
    if(x<0){
      wt('-');
      x=-x;
    }
  }
  if(x>=10000000000000000){
    uint32_t r1=x%100000000;
    uint64_t q1=x/100000000;
    uint32_t r2=q1%100000000;
    uint32_t q2=q1/100000000;
    uint32_t n1=r1%10000,n2=r1/10000,n3=r2%10000,n4=r2/10000;
    if(x>=1000000000000000000){
      if constexpr(std::is_unsigned_v<T>){
        if(x>=10000000000000000000ull){
          obuf[por++]='1';
        }
      }
      memcpy(obuf+por,pre.t+(q2<<2)+1,3);
      memcpy(obuf+por+3,pre.t+(n4<<2),4);
      memcpy(obuf+por+7,pre.t+(n3<<2),4);
      memcpy(obuf+por+11,pre.t+(n2<<2),4);
      memcpy(obuf+por+15,pre.t+(n1<<2),4);
      por+=19;
    }
    else if(x>=100000000000000000){
      uint32_t q3=(q2*205)>>11;
      uint32_t r3=q2-q3*10;
      obuf[por]='0'+q3;
      obuf[por+1]='0'+r3;
      memcpy(obuf+por+2,pre.t+(n4<<2),4);
      memcpy(obuf+por+6,pre.t+(n3<<2),4);
      memcpy(obuf+por+10,pre.t+(n2<<2),4);
      memcpy(obuf+por+14,pre.t+(n1<<2),4);
      por+=18;
    }
    else{
      obuf[por]='0'+q2;
      memcpy(obuf+por+1,pre.t+(n4<<2),4);
      memcpy(obuf+por+5,pre.t+(n3<<2),4);
      memcpy(obuf+por+9,pre.t+(n2<<2),4);
      memcpy(obuf+por+13,pre.t+(n1<<2),4);
      por+=17;
    }
  }
  else{
    static char buf[12];
    int i=8;
    while(x>=10000){
      memcpy(buf+i,pre.t+((x%10000)<<2),4);
      x/=10000;
      i-=4;
    }
    if(x<100){
      if(x<10)obuf[por++]='0'+x;
      else{
        obuf[por]='0'+x/10;
        obuf[por+1]='0'+x%10;
        por+=2;
      }
    }
    else{
      if(x<1000){
        memcpy(obuf+por,pre.t+(x<<2)+1,3);
        por+=3;
      }
      else{
        memcpy(obuf+por,pre.t+(x<<2),4);
        por+=4;
      }
    }
    memcpy(obuf+por,buf+i+4,8-i);
    por+=8-i;
  }
}
inline void wt(const std::string&s){
  int sz=s.size();
  if(por+sz>buf_size)flush();
  memcpy(obuf+por,s.c_str(),sz);
  por+=sz;
}
inline void wt(__int128_t x){
  if(por+50>buf_size)flush();
  if(x==0){
    wt('0');
    return;
  }
  if(x<0){
    wt('-');
    x=-x;
  }
  static constexpr __int128_t b=10000000000000000000ull;
  if(x>=b){
    wt<unsigned long long>(x/b);
    unsigned long long y=x%b;
    memcpy(obuf+por,pre.t+((y/10000000000000000)<<2)+1,3);
    memcpy(obuf+por+3,pre.t+((y/1000000000000%10000)<<2),4);
    memcpy(obuf+por+7,pre.t+((y/100000000%10000)<<2),4);
    memcpy(obuf+por+11,pre.t+((y/10000%10000)<<2),4);
    memcpy(obuf+por+15,pre.t+((y%10000)<<2),4);
    por+=19;
  }
  else wt<unsigned long long>(x);
}
struct Dummy{
  Dummy(){std::atexit(flush);}
}dummy;
}
using fastio::rd;
using fastio::wt;
#include<cassert>
template<typename T>
void dft(std::vector<T>&a){
  using value_type=typename T::value_type;
  using mul_type=typename T::mul_type;
  #ifdef NTT_SIMD
  if constexpr(std::numeric_limits<value_type>::digits<=32){
    if((int)a.size()>=32){
      dft_simd(a);
      return;
    }
  }
  #endif
  static constexpr ntt_root<T::mod()>r;
  static constexpr mul_type mod2=(mul_type)T::mod()*T::mod();
  int n=a.size();
  int h=lsb(n);
  int len=0;
  while(len<h){
    if(h-len==1){
      T rot=T::raw(1);
      for(int s=0;s<(1<<len);s++){
        int of=s*2;
        T u=a[of],v=a[of+1]*rot;
        a[of]=u+v;
        a[of+1]=u-v;
        rot*=T::raw(r.rate2[lsb(~(unsigned int)s)]);
      }
      len++;
    }
    else{
      int p=1<<(h-len-2);
      T rot=T::raw(1),imag=T::raw(r.root[2]);
      for(int s=0;s<(1<<len);s++){
        const mul_type rot1=rot.val(),rot2=(rot*rot).val(),rot3=(rot*T::raw(rot2)).val();
        int of=s<<(h-len);
        for(int i=0;i<p;i++){
          const mul_type a0=a[i+of].val(),a1=(mul_type)a[i+of+p].val()*rot1,a2=(mul_type)a[i+of+p*2].val()*rot2,a3=(mul_type)a[i+of+p*3].val()*rot3;
          const mul_type m=(mul_type)T(a1+mod2-a3).val()*imag.val();
          const mul_type k=mod2-a2;
          a[i+of]=a0+a2+a1+a3;
          a[i+of+p]=a0+a2+(mod2*2-a1-a3);
          a[i+of+p*2]=a0+k+m;
          a[i+of+p*3]=a0+k+(mod2-m);
        }
        rot*=T::raw(r.rate3[lsb(~(unsigned int)s)]);
      }
      len+=2;
    }
  }
}
template<typename T>
void idft(std::vector<T>&a){
  using value_type=typename T::value_type;
  using mul_type=typename T::mul_type;
  #ifdef NTT_SIMD
  if constexpr(std::numeric_limits<value_type>::digits<=32){
    if((int)a.size()>=32){
      idft_simd(a);
      return;
    }
  }
  #endif
  static constexpr ntt_root<T::mod()>r;
  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<p;i++){
        T u=a[i],v=a[i+p];
        a[i]=u+v;
        a[i+p]=u-v;
      }
      len--;
    }
    else{
      int p=1<<(h-len);
      T rot=T::raw(1),imag=T::raw(r.invroot[2]);
      for(int s=0;s<(1<<(len-2));s++){
        const mul_type rot1=rot.val(),rot2=(rot*rot).val(),rot3=(rot*T::raw(rot2)).val();
        int of=s<<(h-len+2);
        for(int i=0;i<p;i++){
          const mul_type a0=a[i+of].val(),a1=a[i+of+p].val(),a2=a[i+of+p*2].val(),a3=a[i+of+p*3].val();
          const mul_type k=T((T::mod()+a2-a3)*imag.val()).val();
          a[i+of]=a0+a1+a2+a3;
          a[i+of+p]=(a0+T::mod()-a1+k)*rot1;
          a[i+of+p*2]=(a0+a1+T::mod()*2-a2-a3)*rot2;
          a[i+of+p*3]=(a0+T::mod()*2-a1-k)*rot3;
        }
        rot*=T::raw(r.invrate3[lsb(~(unsigned int)s)]);
      }
      len-=2;
    }
  }
}
template<typename T>
std::vector<T>ntt_convolution(std::vector<T> a,std::vector<T> b){
  int n=a.size(),m=b.size(),s=n+m-1;
  if(std::min(n,m)<60){
    if(n==0||m==0)return {};
    std::vector<T>ret(s,0);
    if(n<m)for(int i=0;i<m;i++)for(int j=0;j<n;j++)ret[i+j]+=a[j]*b[i];
    else for(int i=0;i<n;i++)for(int j=0;j<m;j++)ret[i+j]+=a[i]*b[j];
    return ret;
  }
  int z=ceil_pow2(s);
  a.resize(z,0);
  b.resize(z,0);
  dft(a),dft(b);
  std::vector<T>c(z);
  for(int i=0;i<z;i++)c[i]=a[i]*b[i];
  idft(c);
  T g=T::raw(z).inv();
  for(int i=0;i<s;i++)c[i]*=g;
  return {c.begin(),c.begin()+s};
}
template<typename T>
std::vector<T> fps_inv(const std::vector<T> &a,int deg=-1){
  int n=a.size();
  if(deg==-1)deg=n;
  const T zero=T::raw(0);
  assert(a[0]!=zero);
  std::vector<T> ret(ceil_pow2(deg));
  ret[0]=a[0].inv();
  for(int m=1;m<deg;m<<=1){
    std::vector<T> f(a.begin(),a.begin()+std::min(n,m*2));
    if(f.size()<m*2)f.resize(m*2,0);
    std::vector<T> g(ret);
    f.resize(m*2);
    dft(f);
    g.resize(m*2);
    dft(g);
    for(int i=0;i<m*2;i++)f[i]*=g[i];
    idft(f);
    T inv=T::raw(m*2).inv();
    for(int i=0;i<m;i++)f[i]=zero;
    for(int i=m;i<m*2;i++)f[i]*=inv;
    dft(f);
    for(int i=0;i<m*2;i++)f[i]*=g[i];
    idft(f);
    for(int i=0;i<m*2;i++)f[i]*=inv;
    for(int i=m;i<m*2;i++)ret[i]-=f[i];
  }
  ret.resize(deg);
  return ret;
}
template<typename T>
void ntt_doubling(std::vector<T>&a){
  static constexpr ntt_root<T::mod()>r;
  int n=a.size()/2;
  std::vector<T>b(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<n;i++){
    b[i]*=now;
    now*=zeta;
  }
  dft(b);
  std::copy(b.begin(),b.end(),a.begin()+n);
}
template<typename T>
void transposed_dft(std::vector<T>&a){
  using value_type=typename T::value_type;
  using mul_type=typename T::mul_type;
  #ifdef NTT_SIMD
  if constexpr(std::numeric_limits<value_type>::digits<=32){
    if((int)a.size()>=32){
      transposed_dft_simd(a);
      return;
    }
  }
  #endif
  static constexpr ntt_root<T::mod()>r;
  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<p;i++){
        T u=a[i],v=a[i+p];
        a[i]=u+v;
        a[i+p]=u-v;
      }
      len--;
    }
    else{
      int p=1<<(h-len);
      T rot=T::raw(1),imag=T::raw(r.root[2]);
      for(int s=0;s<(1<<(len-2));s++){
        const mul_type rot1=rot.val(),rot2=(rot*rot).val(),rot3=(rot*T::raw(rot2)).val();
        int of=s<<(h-len+2);
        for(int i=0;i<p;i++){
          const mul_type a0=a[i+of].val(),a1=a[i+of+p].val(),a2=a[i+of+p*2].val(),a3=a[i+of+p*3].val();
          const mul_type k=T((T::mod()+a2-a3)*imag.val()).val();
          a[i+of]=a0+a1+a2+a3;
          a[i+of+p]=(a0+T::mod()-a1+k)*rot1;
          a[i+of+p*2]=(a0+a1+T::mod()*2-a2-a3)*rot2;
          a[i+of+p*3]=(a0+T::mod()*2-a1-k)*rot3;
        }
        if(s+1!=1<<(len-2))rot*=T::raw(r.rate3[lsb(~(unsigned int)s)]);
      }
      len-=2;
    }
  }
}
template<typename T>
void transposed_idft(std::vector<T>&a){
  using value_type=typename T::value_type;
  using mul_type=typename T::mul_type;
  #ifdef NTT_SIMD
  if constexpr(std::numeric_limits<value_type>::digits<=32){
    if((int)a.size()>=32){
      transposed_idft_simd(a);
      return;
    }
  }
  #endif
  static constexpr ntt_root<T::mod()>r;
  static constexpr mul_type mod2=(mul_type)T::mod()*T::mod();
  int n=a.size();
  int h=lsb(n);
  int len=0;
  while(len<h){
    if(h-len==1){
      T rot=T::raw(1);
      for(int s=0;s<(1<<len);s++){
        int of=s*2;
        T u=a[of],v=a[of+1]*rot;
        a[of]=u+v;
        a[of+1]=u-v;
        if(s+1!=1<<len)rot*=T::raw(r.invrate2[lsb(~(unsigned int)s)]);
      }
      len++;
    }
    else{
      int p=1<<(h-len-2);
      T rot=T::raw(1),imag=T::raw(r.invroot[2]);
      for(int s=0;s<(1<<len);s++){
        const mul_type rot1=rot.val(),rot2=(rot*rot).val(),rot3=(rot*T::raw(rot2)).val();
        int of=s<<(h-len);
        for(int i=0;i<p;i++){
          const mul_type a0=a[i+of].val(),a1=(mul_type)a[i+of+p].val()*rot1,a2=(mul_type)a[i+of+p*2].val()*rot2,a3=(mul_type)a[i+of+p*3].val()*rot3;
          const mul_type m=(mul_type)T(a1+mod2-a3).val()*imag.val();
          const mul_type k=mod2-a2;
          a[i+of]=a0+a2+a1+a3;
          a[i+of+p]=a0+a2+(mod2*2-a1-a3);
          a[i+of+p*2]=a0+k+m;
          a[i+of+p*3]=a0+k+(mod2-m);
        }
        if(s+1!=1<<len)rot*=T::raw(r.invrate3[lsb(~(unsigned int)s)]);
      }
      len+=2;
    }
  }
}
template<typename T>
std::vector<T>transposed_ntt_convolution(std::vector<T>a,std::vector<T>b){
  assert(a.size()>=b.size());
  int n=a.size(),m=b.size();
  int s=ceil_pow2(n);
  T inv=T(s).inv();
  for(int i=0;i<n;i++)a[i]*=inv;
  a.resize(s),b.resize(s);
  transposed_idft(a);
  dft(b);
  for(int i=0;i<s;i++)a[i]*=b[i];
  transposed_dft(a);
  a.resize(n-m+1);
  return a;
}
template<typename T>
void transposed_ntt_doubling(std::vector<T>&a){
  static constexpr ntt_root<T::mod()>r;
  int n=a.size()/2;
  std::vector<T>b(a.begin(),a.begin()+n);
  a=std::vector<T>(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<n;i++)a[i]*=now,now*=zeta;
  transposed_idft(a);
  for(int i=0;i<n;i++)a[i]+=b[i];
}
#include<algorithm>
template<typename T>
T vandermonde_det(std::vector<T>f){
  int n=ceil_pow2(f.size());
  int log2n=msb(n);
  std::vector<std::vector<T>>f2(log2n+1,std::vector<T>(n*2));
  for(int i=0;i<f.size();i++)f2[0][i*2]=-f[i];
  for(int i=0;i<log2n;i++){
    int b=1<<i;
    std::vector<T>l(b*2),r(b*2);
    for(int j=0;j<n*2;j+=b*4){
      std::copy(f2[i].begin()+j,f2[i].begin()+j+b,l.begin());
      std::copy(f2[i].begin()+j+b*2,f2[i].begin()+j+b*3,r.begin());
      ntt_doubling(l),ntt_doubling(r);
      for(int k=0;k<b;k++)l[k]++,r[k]++;
      for(int k=b;k<b*2;k++)l[k]--,r[k]--;
      std::copy(l.begin(),l.end(),f2[i].begin()+j);
      std::copy(r.begin(),r.end(),f2[i].begin()+j+b*2);
      for(int k=0;k<b*2;k++)f2[i+1][j+k]=l[k]*r[k]-T::raw(1);
    }
  }
  std::vector<std::vector<T>>g(log2n+1,std::vector<T>(n*2));
  {
    std::vector<T>h(f2[log2n].begin(),f2[log2n].begin()+n);
    idft(h);
    T inv=T::raw(h.size()).inv();
    for(T&x:h)x*=inv;
    std::reverse(h.begin(),h.end());
    for(int i=n-1;i>=1;i--)h[i]=h[i-1];
    h[0]=1;
    h=fps_inv(h);
    std::copy(h.begin(),h.end(),g[log2n].begin()+n-1);
  }
  transposed_idft(g[log2n]);
  for(int i=0;i<n*2;i++)g[log2n][i]/=n*2;
  for(int i=log2n-1;i>=0;i--){
    int b=1<<i;
    for(int j=0;j<n*2;j+=b*4){
      std::vector<T>l(f2[i].begin()+j,f2[i].begin()+j+b*2);
      std::vector<T>r(f2[i].begin()+j+b*2,f2[i].begin()+j+b*4);
      std::vector<T>g2(g[i+1].begin()+j,g[i+1].begin()+j+b*4);
      l.resize(b*4);
      r.resize(b*4);
      ntt_doubling(l),ntt_doubling(r);
      for(int k=0;k<b*4;k++)l[k]=g2[k]*l[k]*l[k],r[k]=g2[k]*r[k];
      transposed_ntt_doubling(l);
      transposed_ntt_doubling(r);
      std::copy(l.begin(),l.end(),g[i].begin()+j+b*2);
      std::copy(r.begin(),r.end(),g[i].begin()+j);
    }
  }
  T res=1;
  for(int i=0;i<(int)f.size();i++)res*=g[0][i*2]+g[0][i*2+1];
  return res;
}
#include<iostream>
#include<optional>
#include<utility>
constexpr std::pair<long long,long long>ext_gcd(long long a,long long b){
  if(b==0)return std::make_pair(1,0);
  auto [x,y]=ext_gcd(b,a%b);
  std::swap(x,y);
  return std::make_pair(x,y-a/b*x);
}
template<std::signed_integral T>
constexpr std::pair<T,T> inv_mod(T a,T b){
  a%=b;
  if(a<0)a+=b;
  if(a==0)return std::make_pair(b,0);
  T s=b,t=a;
  T m0=0,m1=1;
  while(t){
    T u=s/t;
    s-=t*u;
    m0-=m1*u;
    std::swap(s,t);
    std::swap(m0,m1);
  }
  if(m0<0)m0+=b/s;
  return std::make_pair(s,m0);
}
template<auto m>
struct modint{
  static_assert(1<=m&&m<(1ull<<63));
  using value_type=std::conditional_t<((m>>31)==0),uint32_t,uint64_t>;
  using mul_type=std::conditional_t<((m>>31)==0),uint64_t,__uint128_t>;
private:
  value_type v;
  static constexpr value_type umod=m;
  constexpr modint sqrt_impl()const{
    if(this->val()<=1)return *this;
    if(umod%8==1){
      modint b=2;
      while(b.pow((umod-1)/2).val()==1)b++;
      value_type m2=umod-1;
      int e=0;
      while(m2%2==0)m2>>=1,e++;
      modint x=this->pow((m2-1)/2);
      modint y=(*this)*x*x;
      x*=*this;
      modint z=b.pow(m2);
      while(y.val()!=1){
        int j=0;
        modint t=y;
        while(t.val()!=1)t*=t,j++;
        z=z.pow((value_type(1))<<(e-j-1));
        x*=z;
        z*=z;
        y*=z;
        e=j;
      }
      return x;
    }
    else if(umod%8==5){
      modint res=this->pow((umod+3)/8);
      if((res*res).val()==this->val())return res;
      else return res*modint(2).pow((umod-1)/4);
    }
    else return this->pow((umod+1)/4);
  }
  template<typename U,std::enable_if_t<std::unsigned_integral<U>||std::is_same_v<U,__uint128_t>,std::nullptr_t> =nullptr>
  static constexpr value_type take_mod(U x){
    if constexpr(std::numeric_limits<U>::max()<umod)return x;
    if constexpr(umod==(1ull<<61)-1){
      value_type res=(x>>61)+(x&umod);
      if(res>=umod)res-=umod;
      return res;
    }
    if constexpr(umod==(1ull<<61)-(1ull<<24)+1){
      static constexpr value_type mask=(1ull<<61)-1;
      value_type high=x>>61,low=x&mask;
      mul_type t=low+(mul_type(high)<<24)-high;
      high=t>>61,low=t&mask;
      low=low+(mul_type(high)<<24)-high;
      if(low>=umod)low-=umod;
      return low;
    }
    return x%umod;
  }
public:
  constexpr modint():v(0){}
  template<typename U,std::enable_if_t<std::signed_integral<U>||std::is_same_v<U,__int128_t>,std::nullptr_t> =nullptr>
  constexpr modint(U x){
    x%=std::make_signed_t<value_type>(umod);
    v=x>=0?x:x+umod;
  }
  template<typename U,std::enable_if_t<std::unsigned_integral<U>||std::is_same_v<U,__uint128_t>,std::nullptr_t> =nullptr>
  constexpr modint(U x):v(take_mod<U>(x)){}
  static constexpr value_type mod(){return umod;}
  template<typename U>
  static constexpr modint raw(U x){
    modint res;
    res.v=x;
    return res;
  }
  constexpr std::make_signed_t<value_type> val()const{return v;}
  constexpr modint &operator+=(const modint&b){
    this->v+=b.v;
    if(this->v>=umod)this->v-=umod;
    return *this;
  }
  constexpr modint &operator-=(const modint&b){
    this->v-=b.v;
    if(this->v>=umod)this->v+=umod;
    return *this;
  }
  constexpr modint &operator*=(const modint&b){
    this->v=take_mod(mul_type(this->v)*mul_type(b.v));
    return *this;
  }
  constexpr modint &operator/=(const modint&b){return *this*=b.inv();}
  constexpr modint operator+()const{return *this;}
  constexpr modint operator-()const{return modint()-*this;}
  friend constexpr modint operator+(const modint&a,const modint&b){return modint(a)+=b;}
  friend constexpr modint operator-(const modint&a,const modint&b){return modint(a)-=b;}
  friend constexpr modint operator*(const modint&a,const modint&b){return modint(a)*=b;}
  friend constexpr modint operator/(const modint&a,const modint&b){return modint(a)/=b;}
  constexpr auto operator<=>(const modint&)const=default;
  constexpr modint operator++(int){
    modint res=*this;
    this->v++;
    if(this->v==umod)this->v=0;
    return res;
  }
  constexpr modint operator--(int){
    modint res=*this;
    if(this->v==0)this->v=umod;
    this->v--;
    return res;
  }
  template<std::integral U>
  constexpr modint pow(U k)const{
    if constexpr(std::is_signed_v<U>){
      assert(0<=k);
    }
    modint res=1,a(*this);
    while(k){
      if(k&1)res*=a;
      a*=a;
      k>>=1;
    }
    return res;
  }
  constexpr modint inv()const{
    if constexpr(isprime_constexpr(umod)){
      if(std::is_constant_evaluated()){
        if(v==0){
          throw "no inverse";
        }
      }
      else assert(v!=0);
      return pow(umod-2);
    }
    else{
      modint res;
      auto [g,x]=inv_mod<std::make_signed_t<value_type>>(this->v,umod);
      if(std::is_constant_evaluated()){
        if(g!=1){
          throw "no inverse";
        }
      }
      else assert(g==1);
      res.v=x;
      return res;
    }
  }
  std::optional<modint>sqrt()const{
    if(this->val()<=1||this->pow((umod-1)/2)==1)return std::make_optional(this->sqrt_impl());
    else return std::nullopt;
  }
  friend std::istream &operator>>(std::istream&is,modint&b){
    long long a;
    is>>a;
    b=modint(a);
    return is;
  }
  friend std::ostream &operator<<(std::ostream&os,const modint&b){
    os<<b.val();
    return os;
  }
};
template<auto m>
struct std::hash<modint<m>>{
  std::size_t operator()(modint<m>x)const{
    return std::hash<typename modint<m>::value_type>(x.val());
  }
};
using mint998=modint<998244353>;
using mint107=modint<1000000007>;
using mint61=modint<2305843009213693951>;
using mint6124=modint<2305843009196916737>;
using mint=mint998;
int main(){
  int n;
  rd(n);
  std::vector<mint>f(n);
  for(mint&x:f){
    int v;
    rd(v);
    x=mint::raw(v);
  }
  std::reverse(f.begin(),f.end());
  wt(vandermonde_det(f).val()),wt('\n');
}
0