Yet Another Mex Problem

 Nayra doesn't like stories of people receiving random arrays as birthday presents, but this time she received random arrays as presents for her own birthday! After struggling for a day trying to figure out what to do with the array, she asked Aryan for help. He gave her the following problem.

Given

integers and

.

  • Let
be the MEX of the array
  • .
  • Let
  • for the array
    • .

    There are

    distinct arrays such that

    .

    Calculate the sum of

    over all such arrays. Since the answer can be huge, print it modulo

    .

    Input Format

    • First line will contain
  • , the number of test cases. Then the test cases follow.
  • Each test case contains a single line of input, three integers
  • and
    • .

    Output Format

    For each test case, output a single line containing the value of the given sum modulo

    .

    Constraints

  • Sum of
  • over all test cases does not exceed
    • .

    Sample Input 1

    6
    1 1 1
    2 2 1
    3 1 2
    2 2 3
    2 2 2
    2 10000000 1
    

    Sample Output 1

    1
    9
    37
    13
    11
    432888326
    

    Explanation

    Test case

    : There are distinct arrays. The arrays are : and

    .

    MEX of . Also,
  • .
  • MEX of . Also,
    • .

    Sum of

    is

    .

    Test case

    : There are distinct arrays. The arrays are : and

    .

    MEX of . Also,
  • .
  • MEX of . Also,
  • .
  • MEX of . Also,
  • .
  • MEX of . Also,
    • .

    Sum of

    is .
    Previous
    Next Post »