Given an array perm of length n which is a permutation of [1, 2, ..., n], return the index of perm in the lexicographically sorted array of all of the permutations of [1, 2, ..., n].
Since the answer may be very large, return it modulo109 + 7.
Example 1:
Input:perm = [1,2]
Output:0
Explanation:
There are only two permutations in the following order:
[1,2], [2,1]
And [1,2] is at index 0.
Example 2:
Input:perm = [3,1,2]
Output:4
Explanation:
There are only six permutations in the following order:
According to the problem requirements, we need to find out how many permutations are lexicographically smaller than the given permutation.
We consider how to calculate the number of permutations that are lexicographically smaller than the given permutation. There are two situations:
The first element of the permutation is less than $perm[0]$, there are $(perm[0] - 1) \times (n-1)!$ permutations.
The first element of the permutation is equal to $perm[0]$, we need to continue to consider the second element, and so on.
The sum of all situations is the answer.
We can use a binary indexed tree to maintain the number of elements that are smaller than the current element in the traversed elements. For the $i$-th element of the given permutation, the number of remaining elements that are smaller than it is $perm[i] - 1 - tree.query(perm[i])$, and the number of permutation types is $(perm[i] - 1 - tree.query(perm[i])) \times (n-i-1)!$, which is added to the answer. Then we update the binary indexed tree and add the current element to the binary indexed tree. Continue to traverse the next element until all elements are traversed.
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the length of the permutation.