<p>For an integer array <code>nums</code>, an <strong>inverse pair</strong> is a pair of integers <code>[i, j]</code> where <code>0 &lt;= i &lt; j &lt; nums.length</code> and <code>nums[i] &gt; nums[j]</code>.</p>

<p>Given two integers n and k, return the number of different arrays consisting of numbers from <code>1</code> to <code>n</code> such that there are exactly <code>k</code> <strong>inverse pairs</strong>. Since the answer can be huge, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 3, k = 0
<strong>Output:</strong> 1
<strong>Explanation:</strong> Only the array [1,2,3] which consists of numbers from 1 to 3 has exactly 0 inverse pairs.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 3, k = 1
<strong>Output:</strong> 2
<strong>Explanation:</strong> The array [1,3,2] and [2,1,3] have exactly 1 inverse pair.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 1000</code></li>
	<li><code>0 &lt;= k &lt;= 1000</code></li>
</ul>

# Data Structures: Dynamic Programming (1D array)
# Algorithm: Dynamic Programming with rolling updates

# Short Intuition:
# We use dynamic programming with rolling updates to track the number of ways to form exactly k inverse pairs.
# By iterating over the size of arrays and maintaining a rolling DP array, we efficiently calculate the result.

# Detailed Intuition:
# - We maintain a dp array where dp[j] represents the number of arrays of a given size with exactly j inverse pairs.
# - For each array size `i`, we update the dp array to account for the new number `i` and the inverse pairs it forms.
# - The key insight is to use cumulative sums to track how the number of inverse pairs changes as numbers are added to the array.
# - For each possible number of inverse pairs `j`, we accumulate the number of valid arrays for size `i - 1` and update dp[j].
# - We also subtract invalid cases where the number of inverse pairs exceeds the possible maximum for the given array size.

# Pseudocode:
# 1. Initialize dp[0] = 1 because there's one way to have 0 inverse pairs.
# 2. For each array size i from 2 to n:
#    - Use cumulative sums to update dp[j] for all j from 1 to k.
#    - Subtract the invalid cases where the number of inverse pairs is greater than i-1.
# 3. Return dp[k], which contains the number of ways to form exactly k inverse pairs.

class Solution:
    def kInversePairs(self, n: int, k: int) -> int:
        MOD = 1000000007
        
        # dp[j] will store the number of ways to have exactly j inverse pairs
        dp = [0] * (k + 1)
        
        # Base case: There is exactly one way to have 0 inverse pairs for any array size
        dp[0] = 1
        
        # Iterate over the size of the array from 2 to n
        for i in range(2, n + 1):
            # Step 1: Accumulate the number of ways to form inverse pairs for array of size i
            for j in range(1, k + 1):
                dp[j] = (dp[j] + dp[j - 1]) % MOD
            
            # Step 2: Subtract invalid cases where there are too many inverse pairs for the array size
            for j in range(k, i - 1, -1):
                dp[j] = (dp[j] - dp[j - i] + MOD) % MOD
        
        # Return the result for exactly k inverse pairs
        return dp[k]

# Time Complexity: O(n * k) - We iterate over n sizes of arrays and for each size, we update k possible inverse pair counts.
# Space Complexity: O(k) - We use a 1D array of size k+1 to store the number of ways to form inverse pairs.


# class Solution:
#     def kInversePairs(self, n: int, k: int) -> int:
#         MOD = 10**9 + 7
#         @cache
#         def dp(n, k):
#             if k == 0:
#                 # there is exactly one way to arrange the array (in increasing order)
#                 return 1
#             if k < 0:
#                 # k in negative area - ivalid
#                 return 0
#             if n == 1:
#                 # can't create any (k) pairs if array has only 1 element
#                 return 0
            
#             return (dp(n - 1, k) + dp(n, k - 1) - dp(n - 1, k - n)) % MOD
#         return dp(n, k)

16600