My Solution
\n/**\n * Time Complexity: O(n * max_num)\n * Space Complexity: O(max_num)\n * where `n` is the length of the vector `nums`\n *       `max_num` is the maximum n
```\n/**\n * Time Complexity: O(n * max_num)\n * Space Complexity: O(max_num)\n * where `n` is the length of the vector `nums`\n *       `max_num` is the maximum number of the vector `nums`\n */\nclass Solution {\n public:\n  int countOfPairs(const vector<int> &nums) {\n    constexpr int mod = 1000000007;\n    constexpr int range = 2;\n    const int n = static_cast<int>(nums.size());\n    const int max_num = *max_element(nums.begin(), nums.end());\n    int dp[range][max_num + 1];\n    memset(dp, 0, sizeof(dp));\n    int previous = 0;\n    int current = 1;\n    for (int j = 0; j < nums.front() + 1; ++j) {\n      dp[previous][j] = 1;\n    }\n    \n    for (int i = 1; i < n; ++i) {\n      int presums[max_num + 2];\n      memset(presums, 0, sizeof(presums));\n      for (int j = 0; j < nums[i - 1] + 1; ++j) {\n        presums[j + 1] = (presums[j] + dp[previous][j]) % mod;\n      }\n      \n      for (int j = 0; j < nums[i] + 1; ++j) {\n        const int max_k = max(-1, min(\n          /**\n           * `k` <= `j` because arr1 is monotonically non-decreasing\n           */\n          j,\n          /**\n           * `nums[i - 1]` - `k` >= `nums[i]` - `j`  because arr2 is monotonically non-increasing\n           * so `k` <= `nums[i - 1]` - `nums[i]` + `j`\n           */\n          nums[i - 1] - nums[i] + j\n        ));\n        dp[current][j] = presums[max_k + 1];\n      }\n      \n      previous ^= 1;\n      current ^= 1;\n    }\n    \n    int ret = 0;\n    for (int j = 0; j < nums.back() + 1; ++j) {\n      ret = (ret + dp[previous][j]) % mod;\n    }\n    return ret;\n  }\n};\n```
[]