Minimum Permutation

5

1 votes
C++, Math, Combinatorics, Arrays, Inclusion-exclusion
Problem

You are given an array A of length N. Your task is to find the number of permutations of length N from which you can construct the given array A using the following rule:

  • For each element A[i] in the array A, it must be equal to the minimum value among the elements in positions 1 through i of the permutation P i.e. A[i]=min(P[1],P[2],...,P[i]).

Since the answer could be a large number, you need to compute it modulo 109+7.

A permutation is a sequence of integers from 1 to N of length N containing each number exactly once. For example, [1],[4,3,5,1,2],[2,3,1] are permutations and [1,1],[4,3,1],[2,3,4] are not.

Input format

  • The first line contains an integer T, denoting the number of test cases.
  • The first line of each test casecontains an integer N.
  • The next line of each test case contains N space-separated integers, elements of array A.

Output format

For each test case, print the number of permutations of length N from which you can construct the given array A. Since the answer could be very large, print it modulo 109+7.

Constraints

1T101N1051A[i]N

 

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For test case 1: No permutation satisfies the condition. Hence, the answer is 0.

For test case 2: The only permutation will be [5,4,3,2,1]. Hence, the answer is 1.

Editor Image

?