The Third Problem solution codeforces-

The Third Problem solution codeforces-

Solution – CLICK HERE

You are given a permutation 1,2,,a1,a2,…,an of integers from 00 to 1n−1. Your task is to find how many permutations 1,2,,b1,b2,…,bn are similar to permutation a.

Two permutations a and b of size n are considered similar if for all intervals [,][l,r] (11≤l≤r≤n), the following condition is satisfied:

The Third Problem solution codeforces

MEX([,+1,,])=MEX([,+1,,]),MEX⁡([al,al+1,…,ar])=MEX⁡([bl,bl+1,…,br]),

where the MEXMEX of a collection of integers 1,2,,c1,c2,…,ck is defined as the smallest non-negative integer x which does not occur in collection c. 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 n is an array consisting of n distinct integers from 00 to 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 =3n=3 and there is a 33 in the array.

Input

The Third Problem solution codeforces

Each test contains multiple test cases. The first line of input contains one integer t (11041≤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 n (11051≤n≤105) — the size of permutation a.

The second line of each test case contains n distinct integers 1,2,,a1,a2,…,an (0<0≤ai<n) — the elements of permutation a.

It is guaranteed that the sum of n across all test cases does not exceed 105105.

Output

For each test case, print a single integer, the number of permutations similar to permutation a, taken modulo 109+7109+7.

Example

input

Copy

The Third Problem solution codeforces

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 =[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 =[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].

Solution – CLICK HERE

Leave a Comment