You are given a permutation a1,a2,…,ana1,a2,…,an of integers from 00 to n−1n−1. Your task is to find how many permutations b1,b2,…,bnb1,b2,…,bn are similar to permutation aa.
Two permutations aa and bb of size nn are considered similar if for all intervals [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n), the following condition is satisfied:
MEX([al,al+1,…,ar])=MEX([bl,bl+1,…,br]),MEX([al,al+1,…,ar])=MEX([bl,bl+1,…,br]),
where the MEXMEX of a collection of integers c1,c2,…,ckc1,c2,…,ck is defined as the smallest non-negative integer xx which does not occur in collection cc. For example, MEX([1,2,3,4,5])=0MEX([1,2,3,4,5])=0, and MEX([0,1,2,4,5])=3MEX([0,1,2,4,5])=3.
Since the total number of such permutations can be very large, you will have to print its remainder modulo 109+7109+7.
In this problem, a permutation of size nn is an array consisting of nn distinct integers from 00 to n−1n−1 in arbitrary order. For example, [1,0,2,4,3][1,0,2,4,3] is a permutation, while [0,1,1][0,1,1] is not, since 11 appears twice in the array. [0,1,3][0,1,3] is also not a permutation, since n=3n=3 and there is a 33 in the array.
Input
Each test contains multiple test cases. The first line of input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. The following lines contain the descriptions of the test cases.
The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the size of permutation aa.
The second line of each test case contains nn distinct integers a1,a2,…,ana1,a2,…,an (0≤ai<n0≤ai<n) — the elements of permutation aa.
It is guaranteed that the sum of nn across all test cases does not exceed 105105.
Output
For each test case, print a single integer, the number of permutations similar to permutation aa, taken modulo 109+7109+7.
Example
input
Copy
5 5 4 0 3 2 1 1 0 4 0 1 2 3 6 1 2 4 0 5 3 8 1 3 7 2 5 0 6 4
output
Copy
2 1 1 4 72
Note
For the first test case, the only permutations similar to a=[4,0,3,2,1]a=[4,0,3,2,1] are [4,0,3,2,1][4,0,3,2,1] and [4,0,2,3,1][4,0,2,3,1].
For the second and third test cases, the given permutations are only similar to themselves.
For the fourth test case, there are 44 permutations similar to a=[1,2,4,0,5,3]a=[1,2,4,0,5,3]:
- [1,2,4,0,5,3][1,2,4,0,5,3];
- [1,2,5,0,4,3][1,2,5,0,4,3];
- [1,4,2,0,5,3][1,4,2,0,5,3];
- [1,5,2,0,4,3][1,5,2,0,4,3].
题意:给你一个数组,找出数组b,满足对于所有的l~r,a[l]~a[r]的最小非负整数等于b[l]~b[r]的最小非负整数。
思路:当一个区间里的最小非负整数确定是x的时候,x-1就可以在区间l~r里放进空出来的位置,空出来的位置就是这个区间的长度减去这个数,那么x就在这个区间的外面,x的位置确定了,因为x确定了新的区间的l~r的mex值,那么当我们遇到在x在这个区间里时,就x可以放的位置数相乘,举个例子,比如 1 2 5 6 0 3 4这组数来说,1~0的最小非负整数是2,2可以放的位置就是5-2=3个位置,即原本的2,5,6的位置,而3不在这个区间,那么3的位置就确定了只能是1。我们在写的时候要用数组a[x]来记录x所在的下标,维护l~r这个区间。刚开始lr设为0所在的坐标,然后我们列举i从1~n-1,然后看看每次i的下标在不在l~r里,如果在就说明可以放空出来的位置,就乘一个r-l+1-i,如果不在就说明他的位置已经确定,更新lr。
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
using namespace std;
const int mod=1e9+7;
const int N=1e5+5;
typedef long long ll;
int n,m;
int a[N];
void sove(){
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
a[x]=i;
}
ll ans=1;
int l=a[0],r=a[0];
for(int i=1;i<n;i++){
if(a[i]<l){
l=a[i];
}else if(a[i]>r){
r=a[i];
}else{
ans=(ans*(ll)(r-l+1-i))%mod;
}
}
cout<<ans<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin.tie() ,cout.tie() ;
int t;
cin>>t;
while(t--){
sove();
}
return 0;
}