Imagine you're a chess tournament organizer tasked with placing k identical pieces on a rectangular m × n grid. Your goal is to calculate the total Manhattan distance between every pair of pieces across all possible valid arrangements.
A valid arrangement means placing all k pieces on the grid with at most one piece per cell. The Manhattan distance between two cells (x₁, y₁) and (x₂, y₂) is calculated as |x₁ - x₂| + |y₁ - y₂|.
Since the number of arrangements can be enormous (think millions of ways to place pieces!), you need to find the sum of Manhattan distances between every pair of pieces over ALL valid arrangements.
Example: With a 2×2 grid and 2 pieces, you could place them at positions (0,0) and (0,1), or (0,0) and (1,0), etc. Calculate the Manhattan distance for each pair in each arrangement, then sum everything up!
Return the result modulo 10⁹ + 7 since the answer can be astronomically large.
Input & Output
Constraints
- 1 ≤ m, n ≤ 50
- 1 ≤ k ≤ m × n
- The answer should be returned modulo 109 + 7
- k represents the number of identical pieces to be placed