DreamGrid has an interesting permutation of 1,2,…,n1,2,…,n denoted by a1,a2,…,ana1,a2,…,an. He generates three sequences ff, gg and hh, all of length nn, according to the permutation aa in the way described below:
- For each 1≤i≤n1≤i≤n, fi=max{a1,a2,…,ai}fi=max{a1,a2,…,ai};
- For each 1≤i≤n1≤i≤n, gi=min{a1,a2,…,ai}gi=min{a1,a2,…,ai};
- For each 1≤i≤n1≤i≤n, hi=fi−gihi=fi−gi.
BaoBao has just found the sequence hh DreamGrid generates and decides to restore the original permutation. Given the sequence hh, please help BaoBao calculate the number of different permutations that can generate the sequence hh. As the answer may be quite large, print the answer modulo 109+7109+7.
Input
The input contains multiple cases. The first line of the input contains a single integer TT (1≤T≤200001≤T≤20000), the number of cases.
For each case, the first line of the input contains a single integer nn (1≤n≤1051≤n≤105), the length of the permutation as well as the sequences. The second line contains nn integers h1,h2,…,hnh1,h2,…,hn (1≤i≤n,0≤hi≤1091≤i≤n,0≤hi≤109).
It's guaranteed that the sum of nn over all cases does not exceed 2⋅1062⋅106.
Output
For each case, print a single line containing a single integer, the number of different permutations that can generate the given sequence hh. Don't forget to print the answer modulo 109+7109+7.
Input
2 welcome toparticipate inthe ccpccontest inharbin inoctober harvest belong ninja reset amazing intriguing
Output
No Yes
题解
#include <bits/stdc++.h>
using namespace std;
long long a[100000+10];
const long long mod=1000000000+7;
int main()
{
int t;
std::ios::sync_with_stdio(false);
cin>>t;
while(t--)
{
int n;
cin>>n;
memset(a,0,sizeof(a));
long long sum=1,num=0;
int flag=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]<a[i-1]||a[i]>=n) flag=1;
}
if(a[0]!=0||flag)
{
cout<<0<<endl;
}
else
{
for(int i=1;i<n;i++)
{
if(a[i]>a[i-1]&&a[i]<n)
{
sum*=2;
num+=(a[i]-a[i-1]-1);
sum=sum%mod;
}
else if(a[i]==a[i-1]&&a[i]<n)
{
sum=sum*num;
sum=sum%mod;
num--;
}
}
cout<<sum%mod<<endl;
}
}
return 0;
}