<p>You are given an array of <strong>positive</strong> integers <code>nums</code> of length <code>n</code>.</p>

<p>We call a pair of <strong>non-negative</strong> integer arrays <code>(arr1, arr2)</code> <strong>monotonic</strong> if:</p>

<ul>
	<li>The lengths of both arrays are <code>n</code>.</li>
	<li><code>arr1</code> is monotonically <strong>non-decreasing</strong>, in other words, <code>arr1[0] &lt;= arr1[1] &lt;= ... &lt;= arr1[n - 1]</code>.</li>
	<li><code>arr2</code> is monotonically <strong>non-increasing</strong>, in other words, <code>arr2[0] &gt;= arr2[1] &gt;= ... &gt;= arr2[n - 1]</code>.</li>
	<li><code>arr1[i] + arr2[i] == nums[i]</code> for all <code>0 &lt;= i &lt;= n - 1</code>.</li>
</ul>

<p>Return the count of <strong>monotonic</strong> pairs.</p>

<p>Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [2,3,2]</span></p>

<p><strong>Output:</strong> <span class="example-io">4</span></p>

<p><strong>Explanation:</strong></p>

<p>The good pairs are:</p>

<ol>
	<li><code>([0, 1, 1], [2, 2, 1])</code></li>
	<li><code>([0, 1, 2], [2, 2, 0])</code></li>
	<li><code>([0, 2, 2], [2, 1, 0])</code></li>
	<li><code>([1, 2, 2], [1, 1, 0])</code></li>
</ol>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">nums = [5,5,5,5]</span></p>

<p><strong>Output:</strong> <span class="example-io">126</span></p>
</div>

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

<ul>
	<li><code>1 &lt;= n == nums.length &lt;= 2000</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 1000</code></li>
</ul>

class Solution:
    def countOfPairs(self, nums: List[int]) -> int:
        """
        2000 * 1000 * 1000
                      |__|

        i, pi

        f[i][ci]
               0 1 2 3 4 5
        (2) 0  1 1 1 0 0 0     
        (3) 1  0 1 2 0 0 0   
        (2) 2  0 1 3 0 0 0

        2
        3 
        j

        k sao cho
            
            j >= k
            nums[i] - j <= nums[i - 1] - k

            j - nums[i] >= k - nums[i - 1]
            
            k <= j - nums[i] + nums[i - 1]
            k <= j

        f[0][ci] = 1 for ci in range(nums[i] + 1)

        f[i][ci]
            ci must larger or equal prev 
                f[i - 1][c < ci]
            cd must smaller or equal than other cd    

        2   0   2
            1   1
            2   0

        3   0   3
            1   2
            2   1
            3   0 

        """
        MOD = 10**9 + 7
        n = len(nums)

        f = [[0 for _ in range(max(nums) + 2)] for __ in range(n)]
        pf = [[0 for _ in range(max(nums) + 2)] for __ in range(n)]

        for j in range(nums[0] + 1):
            f[0][j] = 1
            pf[0][j] = (pf[0][j - 1] + f[0][j]) % MOD

        for i in range(1, n):
            for j in range(nums[i] + 1):
                k = min(
                    j,
                    j - nums[i] + nums[i - 1]
                )
                if k >= 0:
                    f[i][j] = pf[i - 1][k] % MOD
                pf[i][j] = (pf[i][j - 1] + f[i][j]) % MOD
        
        return pf[n - 1][nums[-1]]

177762