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

/* ★ _____                           _         ★*/
/* ★|__  / __   __   ___   ____   __| |  _   _ ★*/
/* ★  / /  \ \ / /  / _ \ |_  /  / _  | | | | |★*/
/* ★ / /_   \ V /  |  __/  / /  | (_| | | |_| |★*/
/* ★/____|   \_/    \___| /___|  \__._|  \__, |★*/
/* ★                                     |___/ ★*/
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << #x << " = " << x << endl
inline const auto optimize = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0), std::cout.tie(0);
    return 0;
}();

const int N = 1000;
const int K = 1000;
const int MODE = 1e9 + 7;
array<array<long long, K + 1>, N + 1> dp;
// 考虑前i个数，此时有j个逆序对的插法

class Solution {
   public:
    int kInversePairs(int n, int k) {
        for (int i = 1; i <= n; ++i) {
            fill(dp[i].begin() + 1, dp[i].begin() + k + 1, 0);
        }
        dp[0][0] = 1;
        // 原本有的逆序对加上新增的逆序对
        // 考虑从小到大这么遍历数字，考察在每一处可能的位置插入我们当前考虑的数字
        // 怎么进行转移？
        for (int i = 1; i <= n; ++i) {
            dp[i][0] = 1;
            for (int j = 1; j <= k; ++j) {
                // 考虑i-1个数字的时候，原本已经有多少个逆序对？
                if (i > j) {
                    // 依赖从0加到j
                    for (int k = 0; k <= j; ++k) {
                        dp[i][j] += dp[i - 1][k];
                        dp[i][j] %= MODE;
                    }
                } else {
                    // 依赖从j-i-1加到j
                    for (int k = j - i + 1; k <= j; ++k) {
                        dp[i][j] += dp[i - 1][k];
                        dp[i][j] %= MODE;
                    }
                }
            }
        }
        return dp[n][k];
    }
};
// 这种逆序对正序对插入的问题，把数字从小到大枚举有奇效
// 然后可以按照插入位置来进行状态转移
15200