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



import numpy as np

class Solution:
    def __init__(self):
        self.results = np.zeros([1,1])
        self.mod = 1000000007


    def count_pairs_new(self, arr1_num, arr2_num, nums:list[int], start_idx, max_values, min_values):
        if len(nums) - start_idx == 0:
            return 1
        if arr1_num!= None and arr1_num > min_values[start_idx]:
            return 0
        if arr1_num!= None and arr1_num > nums[start_idx]:
            return 0
        else:
            if arr1_num != None and arr2_num != None and self.results[start_idx,arr1_num,arr2_num] != -1:
                return self.results[start_idx,arr1_num,arr2_num]
            max_arr1 = min([nums[start_idx], max_values[start_idx]])
            min_arr1 = max(arr1_num, nums[start_idx] - arr2_num) if arr2_num!=None else 0
            total_num=0
            for item1 in range(min_arr1, max_arr1+1):
                total_num = (total_num + self.count_pairs_new(item1, nums[start_idx]-item1, nums, start_idx + 1, max_values, min_values)) % self.mod
            if arr1_num != None and arr2_num != None:
                self.results[start_idx,arr1_num,arr2_num] = total_num
            return total_num

    def countOfPairs(self, nums: List[int]) -> int:
        min_list, max_list = [], []
        for i in range(len(nums)):
            min_list.append(min(nums[i:]))
            max_list.append(max(nums[i:]))
        self.results = np.ones([len(nums), 51, 51], dtype=int)*-1
        return self.count_pairs_new(None, None, nums, 0, max_list, min_list)

81315