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

'''
1,2 - 0
2,1 - 1

3,1,2 - 2
3,2,1 - 3

2,1,3 - 1
2,3,1 - 2

1,2,3 - 0
1,3,2 - 1

4,3,1,2 - 5
4,3,2,1 - 6
4,2,1,3 - 4
4,2,3,1 - 5
4,1,2,3 - 3
4,1,3,2 - 4

3,4,1,2 - 4
3,4,2,1 - 5
3,2,1,4 - 3
3,2,4,1 - 4
3,1,2,4 - 2
3,1,4,2 - 3

2,4,1,3 - 3
2,4,3,1 - 4
2,3,1,4 - 2
2,3,4,1 - 3
2,1,3,4 - 1
2,1,4,3 - 2

1,4,2,3 - 2
1,4,3,2 - 3
1,3,2,4 - 1
1,3,4,2 - 2
1,2,3,4 - 0
1,2,4,3 - 1


  0, 1, 2, 3, 4, 5, 6, 7, 8
1 1
2 1  1
3 1  2. 2. 1
4 1  3. 5. 6. 5. 3. 1

'''
class Solution:
    def kInversePairs(self, n: int, k: int) -> int:
        if k == 0:
            return 1
        if k == 1:
            return n - 1
        ar = [1]
        #print(1, ar)
        for i in range(2, n + 1):
            newAr = [1]
            j = 1
            nextVal = 1
            while nextVal > 0 and j <= k:
                #print('  ', i, j, nextVal, newAr)
                if j < len(ar):
                    nextVal += ar[j]
                if j >= i:
                    nextVal -= ar[j - i]
                newAr.append(nextVal)
                j += 1
            ar = newAr
            #print(i, ar)
        return ar[k] % 1000000007 if k < len(ar) else 0


17300