Minimum Permutation
Practice
5 (1 votes)
C++
Math
Combinatorics
Arrays
Inclusion Exclusion
Problem
80% Success 536 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(10^9+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 \(10^9+7\).

Constraints

\(1 \leq T \leq 10 \\ 1 \leq N \leq 10^5 \\ 1 \leq A[i] \leq N\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
6 votes
Tags:
ApprovedDynamic ProgrammingFast Fourier transformHardMathOpen
Points:50
5 votes
Tags:
CombinatoricsInclusion-exclusionC++Math
Points:50
Tags:
Centroid decompositionDisjoint setHardInclusion ExclusionInclusion exclusion principleMathTrees