values, add up the elements of every staircase piece (contiguous subarray) together. The answer can be large, so return it modulo 10⁹ + 7.
Enumerating all pieces is too slow; aim for O(n).values=[4, 5, 6] → 50
The fitting pieces are [4], [5], [6], [4, 5], [5, 6], [4, 5, 6]. Their sums: 4 + 5 + 6 + 9 + 11 + 15 = 50.
values=[3, 2, 2, 3] → 20
Besides single elements (total 10), [3, 2] (step −1) and [2, 3] (step +1) fit: 10 + 5 + 5 = 20. [2, 2] does not, its step is 0.
values=[10, 30] → 40
Only single elements fit: 10 + 30 = 40.
values=[4, 5, 6]
50
Run checks the first 3 tests in your browser. Submit checks all 124 tests on the server, hidden ones included.