考虑加一个
n
+
1
n+1
n+1的
然后转成左上角走到右下角
直接没法分治
n
t
t
ntt
ntt
考虑枚举将
a
[
m
i
d
]
a[mid]
a[mid]作为矩阵划分出来
然后转移
那么就是左/上转移到右/下
设高为
n
n
n,长为
m
m
m
上
→
下
:
上\rightarrow 下:
上→下:
P
(
x
)
=
∑
i
=
0
m
−
1
∑
j
=
0
i
a
j
(
i
−
j
+
n
−
1
n
−
1
)
x
i
P(x)=\sum_{i=0}^{m-1}\sum_{j=0}^ia_{j}{i-j+n-1\choose n-1}x^i
P(x)=∑i=0m−1∑j=0iaj(n−1i−j+n−1)xi
左
→
右
:
左\rightarrow 右:
左→右:
Q
(
x
)
=
∑
i
=
0
n
−
1
∑
j
=
0
i
b
j
(
i
−
j
+
m
−
1
m
−
1
)
x
i
Q(x)=\sum_{i=0}^{n-1}\sum_{j=0}^ib_j{i-j+m-1\choose m-1}x^i
Q(x)=∑i=0n−1∑j=0ibj(m−1i−j+m−1)xi
左
→
下
:
左\rightarrow 下:
左→下:
P
(
x
)
=
∑
i
=
0
m
−
1
∑
j
=
0
n
−
1
a
j
(
i
−
j
+
n
−
1
i
)
x
i
P(x)=\sum_{i=0}^{m-1}\sum_{j=0}^{n-1}a_j{i-j+n-1\choose i}x^i
P(x)=∑i=0m−1∑j=0n−1aj(ii−j+n−1)xi
x
n
−
1
P
(
x
)
=
∑
i
=
n
−
1
n
+
m
−
2
x
i
(
i
−
n
+
1
)
!
∑
j
=
0
n
−
1
a
j
(
n
−
j
−
1
)
!
(
i
−
j
)
!
x^{n-1}P(x)=\sum_{i=n-1}^{n+m-2}\frac{x^i}{(i-n+1)!}\sum_{j=0}^{n-1}\frac{a_j}{(n-j-1)!}(i-j)!
xn−1P(x)=∑i=n−1n+m−2(i−n+1)!xi∑j=0n−1(n−j−1)!aj(i−j)!
上
→
右
上\rightarrow 右
上→右
Q
(
x
)
=
∑
i
=
0
n
=
1
∑
j
=
0
m
−
1
a
j
(
i
+
m
−
1
−
j
i
)
x
i
Q(x)=\sum_{i=0}^{n=1}\sum_{j=0}^{m-1}a_j{i+m-1-j\choose i}x^i
Q(x)=∑i=0n=1∑j=0m−1aj(ii+m−1−j)xi
x
m
−
1
Q
(
x
)
=
∑
i
=
m
−
1
n
+
m
−
2
x
i
(
i
−
m
+
1
)
!
∑
j
=
0
m
−
1
a
j
(
m
−
j
−
1
)
!
(
i
−
j
)
!
x^{m-1}Q(x)=\sum_{i=m-1}^{n+m-2}\frac{x^i}{(i-m+1)!}\sum_{j=0}^{m-1}\frac{a_j}{(m-j-1)!}(i-j)!
xm−1Q(x)=∑i=m−1n+m−2(i−m+1)!xi∑j=0m−1(m−j−1)!aj(i−j)!
直接做即可
复杂度
O
(
n
l
o
g
2
n
)
O(nlog^2n)
O(nlog2n)
#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define y1 shinkle
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
s[top+1]='\0';return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a=max(a,b);}
template<typename tp>inline void chemn(tp &a,tp b){a=min(a,b);}
cs int mod=998244353;
inline int add(int a,int b){return (a+b)>=mod?(a+b-mod):(a+b);}
inline int dec(int a,int b){return (a<b)?(a-b+mod):(a-b);}
inline int mul(int a,int b){static ll r;r=(ll)a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){a=(a+b)>=mod?(a+b-mod):(a+b);}
inline void Dec(int &a,int b){a=(a<b)?(a-b+mod):(a-b);}
inline void Mul(int &a,int b){static ll r;r=(ll)a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(ll x){x%=mod;return (x<0)?x+mod:x;}
typedef vector<int> poly;
namespace Poly{
cs int C=19,M=(1<<C)|5;
int rev[M],*w[C+1],fac[M],ifac[M];
inline void init_rev(int lim){
for(int i=0;i<lim;i++)rev[i]=(rev[i>>1]>>1)|((i&1)*(lim>>1));
}
inline void init_w(){
for(int i=1;i<=C;i++)w[i]=new int [(1<<(i-1))|1];
int wn=ksm(3,(mod-1)/(1<<C));w[C][0]=1;
for(int i=1,l=1<<(C-1);i<l;i++)w[C][i]=mul(w[C][i-1],wn);
for(int i=C-1;i;i--)
for(int j=0,l=1<<(i-1);j<l;j++)w[i][j]=w[i+1][j<<1];
fac[0]=ifac[0]=1;
for(int i=1;i<M;i++)fac[i]=mul(fac[i-1],i);
ifac[M-1]=Inv(fac[M-1]);
for(int i=M-2;i;i--)ifac[i]=mul(ifac[i+1],i+1);
}
inline void ntt(int *f,int lim,int kd){
for(int i=0;i<lim;i++)if(i>rev[i])swap(f[i],f[rev[i]]);
for(int mid=1,l=1,a0,a1;mid<lim;mid<<=1,l++)
for(int i=0;i<lim;i+=mid<<1)
for(int j=0;j<mid;j++)
a0=f[i+j],a1=mul(f[i+j+mid],w[l][j]),f[i+j]=add(a0,a1),f[i+j+mid]=dec(a0,a1);
if(kd==-1){
reverse(f+1,f+lim);
for(int i=0,iv=Inv(lim);i<lim;i++)Mul(f[i],iv);
}
}
inline poly operator *(poly a,poly b){
if(!a.size()||!b.size())return poly(0);
int deg=a.size()+b.size()-1;
if(a.size()<=20||b.size()<=20){
poly c(deg,0);
for(int i=0;i<a.size();i++)
for(int j=0;j<b.size();j++)
Add(c[i+j],mul(a[i],b[j]));
return c;
}int lim=1;while(lim<deg)lim<<=1;
init_rev(lim);
a.resize(lim),ntt(&a[0],lim,1);
b.resize(lim),ntt(&b[0],lim,1);
for(int i=0;i<lim;i++)Mul(a[i],b[i]);
ntt(&a[0],lim,-1),a.resize(deg);return a;
}
}
inline int Cb(int n,int m){
using namespace Poly;
return (n<m||n<0||m<0)?0:mul(fac[n],mul(ifac[m],ifac[n-m]));
}
cs int N=200005;
map<int,int> dp[N];
int a[N],n;
inline void trans(int l,int r,int dn,int up){
using namespace Poly;
if(l==1&&r==1&&up==n+1){
for(int i=dn;i<=up;i++)dp[1][i]=1;
return;
}
int n=up-dn+1,m=r-l+1;
poly a(n),b(m);//a->left b->top
poly p(n),q(m);//p->rigjt q->bottom
for(int i=0;i<m;i++)b[i]=dp[l+i][up+1];
for(int i=0;i<n;i++)a[i]=dp[l-1][up-i];
poly f,g;
f=a,g.resize(n);
for(int i=0;i<n;i++)g[i]=Cb(i+m-1,m-1);
f=f*g;
for(int i=0;i<n;i++)Add(p[i],f[i]);
f=b,g.resize(m);
for(int i=0;i<m;i++)g[i]=Cb(i+n-1,n-1);
f=f*g;
for(int i=0;i<m;i++)Add(q[i],f[i]);
f.resize(n),g.resize(n+m-1);
for(int i=0;i<n;i++)f[i]=mul(a[i],ifac[n-i-1]);
for(int i=0;i<n+m-1;i++)g[i]=fac[i];
f=f*g;
for(int i=0;i<m;i++)Add(q[i],mul(f[i+n-1],ifac[i]));
f.resize(m),g.resize(n+m-1);
for(int i=0;i<m;i++)f[i]=mul(b[i],ifac[m-i-1]);
for(int i=0;i<n+m-1;i++)g[i]=fac[i];
f=f*g;
for(int i=0;i<n;i++)Add(p[i],mul(f[i+m-1],ifac[i]));
for(int i=l;i<=r;i++)Add(dp[i][dn],q[i-l]);
for(int i=up;i>dn;i--)Add(dp[r][i],p[up-i]);
}
void solve(int l,int r,int dn){
if(l>r)return;
int mid=(l+r)>>1,x=mid,y=mid;
while(x>l&&a[x-1]==a[mid])x--;
while(y<r&&a[y+1]==a[mid])y++;
solve(l,x-1,a[mid]+1);
trans(l,y,dn,a[mid]);
solve(y+1,r,dn);
}
inline void solve(){
n=read();
for(int i=1;i<=n;i++)a[i]=read();
a[n+1]=n+1;reverse(a+1,a+n+2);
for(int i=0;i<=n+1;i++)dp[i].clear();
solve(1,n+1,1);
cout<<dp[n+1][1]<<'\n';
}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
Poly::init_w();
int T=read();
while(T--)solve();
return 0;
}