Given an array, find the sum of the minimum element of every contiguous subarray. Return the result modulo .
With [3, 1, 2, 4]:
- Subarrays and their mins:
[3]→3,[1]→1,[2]→2,[4]→4,[3,1]→1,[1,2]→1,[2,4]→2,[3,1,2]→1,[1,2,4]→1,[3,1,2,4]→1. - Sum =
3+1+2+4+1+1+2+1+1+1 = 17.
Instead of generating all subarrays, can you figure out how many times each element contributes as the minimum?
Constraints: .