#K49047. Grid Filling Ways
Grid Filling Ways
Grid Filling Ways
You are given a grid of size \(N \times M\). Your task is to compute the number of ways to fill the grid such that each row and each column contains distinct integers from \(1\) to \(\max(N, M)\). A careful observation shows that a valid filling is possible only when the grid is square, i.e. \(N = M\). In that case, the number of ways is \( (N!)^2 \); otherwise, the answer is \(0\).
inputFormat
The first line contains a single integer \(T\) representing the number of test cases. Then \(T\) lines follow. Each line contains two space-separated integers \(N\) and \(M\), which represent the dimensions of the grid.
outputFormat
For each test case, output the number of valid ways to fill the grid on a separate line.
## sample3
2 2
3 3
2 3
4
36
0
</p>