


Input: 'N' = 4 'K' = 3
Output: 1
There is only one permutation [0, 1, 2, 3] such that a number of elements with 'ARR[I] = I' is 'K' = 3.
The first line of input contains an integer 'T', denoting the number of test cases.
For each test case, there will be only one line containing two integers' N' and 'K'.
For each test case, print the number of permutations modulo 10^9 + 7 satisfying the number of elements with 'ARR[I] = I' is 'K'.
You don't need to print anything. It has already been taken care of. Just implement the given function.
1 <= 'T' <= 10^4
4 <= 'N' <= 10^18
'N - 4' <= 'K' <= 'N'
1 <= K
Time Limit: 1sec
We will maintain a counter based on that and increment it when the current permutation satisfies the condition.
Algorithm :
// It is a recursive function to get all the permutations of the current array 'arr'.
Let's first fix the positions in the array such that 'ARR[I] != I', Say there are 'M' such positions. (0 <= M <= 'N - K')
Let's count the number of permutations with fixed 'M' for that, and we need to choose the indices having the 'ARR[I] !=I'. We can easily find this using the simple combination formula 'NCM'.
Then we need to construct a permutation for chosen indices such that for every chosen index, 'ARR[I] !=I'; This is nothing but the derangements, and we will find this using an exhaustive search.
We will do the casework for this problem to find the derangements according to the 'K' value.
// Driver function to get the modular addition.
int add(a, b)
// Driver function to get the modular multiplication.
// Driver function to get the modular binary exponention.
// Driver function to get the modular division.