Recognized it pretty quickly as a DP problem but still fumbled the transition formula for a bit.
Use dynamic programming where dp[i][j] represents the number of permutations of length i with exactly j inverse pairs. The recurrence is dp[i][j] = sum_{k=0}^{min(j, i-1)} dp[i-1][j-k], which can be optimized with prefix sums to O(m*n) time. Return dp[m][n] modulo 10^9+7.
Pro tip: Mention that the maximum number of inverse pairs for length m is m*(m-1)/2, so if n exceeds this, return 0 immediately. Also, using a 1D DP array and updating in-place can save space.
Define dp[i][j] as the number of permutations of length i with exactly j inverse pairs. Initialize dp[0][0] = 1 and all other dp[0][j] = 0.
When inserting the largest element i into a permutation of length i-1, it can create between 0 and i-1 new inverse pairs. Thus, dp[i][j] = sum_{k=0}^{min(j, i-1)} dp[i-1][j-k].
Compute prefix sums of dp[i-1] to calculate each dp[i][j] in O(1) time, reducing overall time complexity to O(m*n).
If n > m*(m-1)/2, return 0. Apply modulo 10^9+7 at each addition to prevent overflow.
After filling the DP table up to m and n, return dp[m][n] modulo 10^9+7.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.