#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define DREP(i,a,b) for(int i=a;i>=b;--i)
typedef long long ll;
using namespace std;
void File(){
freopen("inverse.in","r",stdin);
freopen("inverse.out","w",stdout);
}
const int maxn=6e5+10;
const int maxw=(1<<19)+10;
const ll mod=998244353;
int T,n,a[maxn];
ll dp[20][maxw];
int bit(int x){return 1<<(x-1);}
int count(int x,int y){return __builtin_popcount(x>>y);}
bool vis[maxn];
ll cal(int x,int len){
memset(dp,0,sizeof(dp));
int all=0,las=0;
REP(i,1,n)if(!vis[i])all|=bit(i);
else if(i!=x)las|=bit(i);
all|=bit(x);
dp[1][bit(x)]=1;
if(count(las,x)>max(n-len+1-x,0))return 0;
REP(i,2,len){
REP(j,1,n){
if(j==x)continue;
if(!(bit(j)&all))continue;
int S0=all^bit(j);
for(int S=S0;S;S=(S-1)&S0){
if(!(bit(x)&S))continue;
if(__builtin_popcount(S)!=i-1)continue;
int low=n-len+i;
if(count(S|las,j)>max(low-j,0))continue;
dp[i][S^bit(j)]=(dp[i][S^bit(j)]+dp[i-1][S])%mod;
}
}
}
return dp[len][all];
}
ll ans;
int main(){
//File();
scanf("%d",&T);
while(T--){
ans=0;
memset(vis,0,sizeof(vis));
scanf("%d",&n);
REP(i,1,n){
scanf("%d",&a[i]);
vis[a[i]]=1;
}
DREP(i,n,1){
vis[a[i]]=0;
bool flag=false;
REP(j,1,i-1){
int cnt=0;
REP(k,1,j-1)if(a[k]>a[j])++cnt;
if(cnt>max(j-a[j],0)){
flag=true;
break;
}
}
if(flag)continue;
REP(j,a[i]+1,n)if(!vis[j]){
vis[j]=1;
ans=(ans+cal(j,n-i+1))%mod;
vis[j]=0;
}
}
printf("%lld\n",ans);
}
return 0;
}
| Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
| Testcase #1 | 6.774 ms | 80 MB + 604 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #2 | 32.218 ms | 80 MB + 604 KB | Wrong Answer | Score: 0 | 显示更多 |
| Testcase #3 | 4 s | 80 MB + 600 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #4 | 6.735 ms | 80 MB + 600 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #5 | 4 s | 80 MB + 604 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #6 | 4 s | 81 MB + 104 KB | Time Limit Exceeded | Score: 0 | 显示更多 |
| Testcase #7 | 54.73 us | 600 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #8 | 55.12 us | 604 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #9 | 56.02 us | 612 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #10 | 56.05 us | 612 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #11 | 55.1 us | 608 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #12 | 58 us | 636 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #13 | 56.36 us | 616 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #14 | 56.5 us | 620 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #15 | 54.64 us | 600 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #16 | 55.87 us | 612 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #17 | 56.21 us | 616 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #18 | 58.71 us | 636 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #19 | 58.35 us | 632 KB | Runtime Error | Score: 0 | 显示更多 |
| Testcase #20 | 54.38 us | 600 KB | Runtime Error | Score: 0 | 显示更多 |