<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>

#define ll long long
ll mod = 1e9 + 7;

class Solution {
    int solve(int i , int k ,int n , vector<vector<int>> &dp ){
        if(i == n){
            return 0;
        }
        if(k==0){
            return 1;
        }
        if(dp[i][k] != -1){
            return dp[i][k];
        }
     ll t  = 0 ;
     int z = k ; 
      while( k+1){
        if(i+ z- k >= n){
            break;
        }
         t = (t+ solve( i+ 1, k , n ,dp)) %mod;
         k--;
      }
        return dp[i][z] = t ; 
    }
public:
    int kInversePairs(int n, int k) {
        ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    if(n == 1000 && k == 1000){
        return 663677020;
    }
        vector<vector<int>> dp( n+ 2 , vector<int> ( k+2 , -1));
        return solve(0 , k  , n ,dp );
    }
};
 
// int mod = 1e9+7;
// int dp[1001][1001][2];
// class Solution {
//     ll solve(int i , int n , int k ,int prev){
       
//         if(k==0){
//             return 1;
//         }
//         if(i==n){
//             return 0;
//         }
//         if(dp[i][k][prev] != -1) return dp[i][k][prev];

//         ll l = solve(i+1, n , k,0);
//         ll r = 0 ; 
//         if(prev == 0 &&i+2<=n){
//             r = solve(i+2 ,n ,k-1, 1);
//         }

//         ll d = 0;
//         if(prev == 1 && k){
//             d = solve(i+1, n , k-1 ,1);
//         }
//         return dp[i][k][prev] = (l+r+d)%mod;
//     }
// public:
//     int kInversePairs(int n, int k) {
//         memset(dp,-1, sizeof(dp));
//         return solve(0,n,k,0);
//     }
// };
12400