#P14022. [ICPC 2024 Nanjing R] Bingo

[ICPC 2024 Nanjing R] Bingo

Description

Given two integers nn, mm and an integer sequence a1,a2,,anma_1, a_2, \cdots, a_{nm} of length n×mn \times m, we're going to fill a grid of nn rows and mm columns with the integers from the sequence. More specifically, let (i,j)(i, j) be the cell on the ii-th row and the jj-th column, we'll put the ((i1)×m+j)((i - 1) \times m + j)-th element of the sequence (that is, a(i1)×m+ja_{(i - 1) \times m + j}) into that cell.

We say an integer kk 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 kk.
  • There is at least one column, where all integers in the cells of that column are less than or equal to kk.

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 (nm)!(nm)! permutations of the given sequence. As the answer may be large, output the answer modulo 998244353998\,244\,353.

Input Format

There are multiple test cases. The first line of the input contains an integer TT indicating the number of test cases. For each test case:

The first line contains two integers nn and mm (1n,m2×1051 \le n, m \le 2 \times 10^5, 1n×m2×1051 \le n \times m \le 2 \times 10^5), indicating the number of rows and columns of the grid.

The second line contains n×mn \times m integers a1,a2,,anma_1, a_2, \cdots, a_{nm} (0ai<9982443530 \le a_i < 998\,244\,353) indicating the given sequence.

It's guaranteed that the sum of n×mn \times m of all test cases will not exceed 4×1054 \times 10^5.

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 11 and 22 are not on the same row or column, then the smallest bingo integer will be 33, otherwise the smallest bingo integer will be 22. There are 88 permutations where 11 and 22 are not on the same row or column, so the answer is 8×3+(4!8)×2=568 \times 3 + (4! - 8) \times 2 = 56.

For the second sample test case, the smallest bingo integer is always 1010, so the answer is 3!×10=603! \times 10 = 60.