<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;= 50</code></li>
</ul>

class Solution:
    def countOfPairs(self, nums: List[int]) -> int:
        """
        """

        mod=10**9+7
        mx=max(nums)
        dp = [0]*(mx+1)
        #for i in range(nums[0]+1):
        #    dp[i]=i+1
        #print(dp)
        dp[:nums[0]+1]=list(range(1, nums[0]+2))
        #print(dp)

        for i in range(1, len(nums)):
            dpi = [0]*(mx+1)
            for j in range(nums[i]+1):
                k = min(j, nums[i-1], j+nums[i-1]-nums[i])
                if k>=0:
                    dpi[j]+=dp[k]
            dpi=list(accumulate(dpi, lambda acc, x:acc+x%mod))
            dp=dpi

        return dp[-1]
        
    def countOfPairs(self, nums: List[int]) -> int:
        """
        https://leetcode.com/problems/find-the-count-of-monotonic-pairs-ii/solutions/5621619/easy-to-understand-dp/
        Approach
dp[i][j] means for the position j, we choose i as the number in arr.

Here,

0≤i≤M
0≤j<n
Take nums = [2,3] as an example, the final answer should be 6 because arr1 can be:

[0,1]
[0,2]
[1,2]
[0,3]
[1,3]
[2,3]
For j == 1, the possible number in arr is 1, 2, and 3. When i == 2, the cases ending with 1 are always valid (i.e. [0, 1] here). Also, we need to add dp[i - d][j - 1] because this is the largest value we can have for the previous digit.

Similarly, when j == 1 and i == 3, the cases ending with 2 are alwyas valid as well (i.e. [0, 2] and [1, 2] here).



        """
        mod=10**9+7
        n, mx=len(nums), max(nums)
        dp = [[0]*(mx+1) for _ in range(n)]

        # dp[i][j] is at pos i, have j as num in arr2
        for i in range(mx+1):
            dp[0][i]=1
        
        for i in range(1, n): 
            d = max(0, nums[i]-nums[i-1])
            for j in range(d, nums[i]+1):
                dp[i][j] = dp[i][j-1]+dp[i-1][j-d]%mod

        #return reduce(lambda acc, i: acc+i%mod, dp[-1], 0)
        return sum(dp[-1])%mod 
22459