dp and "pref-sum"s
Observation. It\'s sufficient to control only arr_1, as the arr_2 is automatically determined by the sum condition (nums[i] = arr_1[i] + arr_2[i])\n\n# Approach
**Observation.** It\'s sufficient to control only `arr_1`, as the `arr_2` is automatically determined by the sum condition (`nums[i] = arr_1[i] + arr_2[i]`)\n\n# Approach\n\nLet `m = max(nums)`, which bounds the values in the arrays due to the non-negativity constraint. We introduce a `dp` array, where `dp[i]` represents the number of ways to create monotonic pairs on the "[:k]"-subarrays. \n\n**Algorithm:**\n1. for each possible value `cur_val` in `arr_1` (ranging from `0` to `m`), iterate over all previous values `prev_val` that maintain the monotonicity of `arr_1` (ranging from `0` to `val-1`).\n2. check if the condition for the monotonicity of `arr_2` is satisfied.\n\n**Optimisation:** in order to pass all the checks, just notice that most of the summations between neighbouring values are repeated. So we can use prefix sums and avoid the internal for-loop from [3250. Find the Count of Monotonic Pairs I](https://leetcode.com/problems/find-the-count-of-monotonic-pairs-i/solutions/5619405/straightforward-dp/).\n\n\n# Complexity\nTime complexity: $$\\mathcal{O}(n \\cdot m \\cdot m)$$\nSpace complexity: $$\\mathcal{O}(m)$$\n\n```python3\ndef countOfPairs(self, nums: List[int]) -> int:\n    M, MOD = max(nums) + 1, 10 ** 9 + 7\n    \n    dp = [0 for _ in range(M)]\n    for i in range(nums[0]+1):\n        dp[i] = 1\n    \n    res = 0\n    for i, num in enumerate(nums[1:], start=1):\n        new_dp = [0 for _ in range(M)]\n        pref_sum = sum(dp)\n        prev_val = M-1\n        for cur_val in range(num, -1, -1):\n            while (nums[i-1] - prev_val < nums[i] - cur_val) or prev_val > cur_val:\n                pref_sum -= dp[prev_val]\n                prev_val -= 1\n            if prev_val >= 0:\n                new_dp[cur_val] += pref_sum % MOD\n        dp = new_dp\n\n    return sum(dp) % MOD\n```
['Python3']