Sum of Subarray Minimums
You are given an array a of n integers. A contiguous subarray is a non-empty run of
consecutive elements a[i], a[i+1], ..., a[j] for some 1 ≤ i ≤ j ≤ n.
For every contiguous subarray, take its minimum element. Sum those minimums over all
n·(n+1)/2 contiguous subarrays, and report the sum modulo 10^9 + 7.
Input
The first line contains a single integer n (1 ≤ n ≤ 3·10^5).
The second line contains n integers a_1, a_2, ..., a_n (1 ≤ a_i ≤ 10^9).
Output
Print a single integer: the sum of the minimums of all contiguous subarrays of a, modulo
10^9 + 7.
Notes
In the first example, the array is [3, 1, 2, 4]. Its subarrays and minimums are:
[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
The total is 3+1+2+4+1+1+2+1+1+1 = 17.
When several subarrays share the same minimum value, be careful not to double-count or miss any of them — the second example uses repeated values to probe exactly that.