#P14022. [ICPC 2024 Nanjing R] Bingo
[ICPC 2024 Nanjing R] Bingo
Description
Given two integers , and an integer sequence of length , we're going to fill a grid of rows and columns with the integers from the sequence. More specifically, let be the cell on the -th row and the -th column, we'll put the -th element of the sequence (that is, ) into that cell.
We say an integer is a bingo integer of the sequence, if after filling all the cells, at least one of the two following conditions is satisfied.
- There is at least one row, where all integers in the cells of that row are less than or equal to .
- There is at least one column, where all integers in the cells of that column are less than or equal to .
It is easy to see that a sequence may have multiple bingo integers, however in this problem, we're only interested in the smallest bingo integer.
Calculate the sum of the smallest bingo integers for all permutations of the given sequence. As the answer may be large, output the answer modulo .
Input Format
There are multiple test cases. The first line of the input contains an integer indicating the number of test cases. For each test case:
The first line contains two integers and (, ), indicating the number of rows and columns of the grid.
The second line contains integers () indicating the given sequence.
It's guaranteed that the sum of of all test cases will not exceed .
Output Format
For each test case, output one line containing one integer indicating the answer.
4
2 2
1 3 2 4
3 1
10 10 10
1 3
20 10 30
3 4
1 1 4 5 1 4 1 9 1 9 8 10
56
60
60
855346687
Hint
For the first sample test case, if and are not on the same row or column, then the smallest bingo integer will be , otherwise the smallest bingo integer will be . There are permutations where and are not on the same row or column, so the answer is .
For the second sample test case, the smallest bingo integer is always , so the answer is .
京公网安备 11011102002149号