Wednesday, 9 February 2022

Count Arrays solution|Codesheff starter 25 for div 3


Given an array A of length N such that 1AiN and Aii, i[1,N].

Count the number of arrays B of length N such that i[1,N]:

  • BiBAi
  • 1BiM

Since the answer may be large, print it modulo 109+7.

Input Format

  • The first line contains a single integer T − the number of test cases. The description of T test cases follows.
  • Each test case contains 2 lines of input:
    • The first line of each test case contains two space separated integers NM.
    • The second line of each test case contains N space separated integers A1,A2,,AN.

Output Format

For each test case, output a single integer on a newline - answer modulo 109+7.

Constraints

  • 1T105
  • 2N105
  • 1M100
  • 1AiN,Aii
  • Sum of N over all test cases does not exceed 106

Sample Input 1 

2
2 3
2 1
3 2
2 1 1

Sample Output 1 

6
2

Explanation

Test Case 1: There are 6 possible arrays - [1,2],[2,1],[1,3],[3,1],[2,3],[3,2].

Test Case 2: There are 2 possible arrays - [1,2,2],[2,1,1].

Solution:

click here for solution