Sum of Subarray Minimums#11612

Sum of Subarray Minimums

1 second
128 megabytes
Medium

Given an array of integers. Find the sum of \min(a_i, a_{i+1}, ..., a_j) for every pair , where 1 \le i \le j \le n.

Input

The first line contains the size n~(1 \le n \le 10^5) of array . The second line contains integers a_i~(1 \le a_i \le 10^5) — the elements of array.

Output

Print the sum of \min(a_i, a_{i+1}, ..., a_j) modulo 10^9 + 7.

Examples

Consider the first test case.

Subarrays are \{3\}, \{1\}, \{2\}, \{4\}, \{3,1\}, \{1,2\}, \{2,4\}, \{3,1,2\}, \{1,2,4\}, \{3,1,2,4\}.

Minimums are 3, 1, 2, 4, 1, 1, 2, 1, 1, 1. Their sum is .

Example 1

Loading
Loading

Example 2

Loading
Loading