Official

B - Counting Grids Editorial by evima


For any way to fill the grid, there is at most one square that violates both of the conditions, because such a square satisfies the following.

  • All numbers written in the column containing that square are less than the number written in that square.
  • All numbers written in the row containing that square are greater than the number written in that square.

Let us count the ways to fill the grid so that there is a square satisfying these two conditions. There are \(N^2\) positions for that square, and \(\binom{N^2}{2N-1}\) sets of numbers to be written in the squares sharing a row or column with that square. For each pair of these, we have \((N-1)!\times (N-1)!\times (N-1)^2!\) ways to fill the grid. Multiplying these counts yields the answer.

posted:
last update: