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

const int mod= 1e9+7;


class Solution {
public:
    long long int factorial[1010];
    long long int dp[1010][1010], pref[1010][1010];

    void init(){
        factorial[0]=1;
        for (int i=1; i<1010; i++){
            factorial[i]= (1LL*i*factorial[i-1])%mod;
        }
    }
    long long int modpow (long long int base, int exponent){
        if (exponent==0) return 1LL;
        if (exponent==1) return base;
        long long int pv= modpow(base, exponent/2);
        pv= (pv*pv)%mod;
        if (exponent%2==1) pv= (pv*base)%mod;
        return pv;
    }

    long long int inverse (long long int base){
        return modpow (base, mod - 2);
    }

    long long int binom (int n, int k){
        long long int denominator = (factorial[k]*factorial[n-k])%mod;
        
        return (factorial[n]*inverse(denominator))%mod;
    }

    long long int get_prefix_sum (int i, int j_1, int j_2){
        if (j_1==0) return pref[i][j_2];
        else return (pref[i][j_2]-pref[i][j_1-1])%mod;
    }
    
    int kInversePairs(int n, int k) {
        for (int i=0; i<=1000; i++){
            for (int j=0; j<=1000; j++) {
                pref[i][j]=0;
                dp[i][j]=0;
            }
        }
        
        dp[1][0]= 1;
        pref[1][0]=1;
        for (int i=1; i<=1000; i++) pref[1][i]= pref[1][i-1];
        for (int i=2; i<=1000; i++){
            dp[i][0]=1;
            for (int j=1; j<=1000; j++){
                if (j<i) dp[i][j]= pref[i-1][j];
                else dp[i][j]= get_prefix_sum(i-1, j-i+1, j);
            }
            pref[i][0]= dp[i][0];
            for (int j=1; j<=1000; j++) pref[i][j]= (pref[i][j-1]+dp[i][j])%mod;
        }
        if (dp[n][k]<0) dp[n][k]+=mod;
     /*   for (int i=1; i<=10; i++){
            for (int j=0; j<=10; j++) cout<<dp[i][j]<<" ";
            cout<<endl;
        }
        cout<<"PREFIXES"<<endl;
        for (int i=1; i<=10; i++){
            for (int j=0; j<=10; j++) cout<<pref[i][j]<<" ";
            cout<<endl;
        }*/
        return dp[n][k];

    }
};
25400