cswork / OAOpen practice workspace

AmazonOA #1

System Vulnerabilities

Estimated HardJudge-ready

Topics (AI-labelled)Stacks & HeapsBits & Math

The vulnerability of each non-empty contiguous segment is its length multiplied by its maximum element. Find the sum of the vulnerabilities of all contiguous segments, modulo 1000000007.

Input

The first line contains n (1..100000); the second line contains n vulnerability values (1..10⁹).

Output

Output one integer, the answer required by the problem.

Example 1

Input:

3
3 1 4

Output:

34

Example 2

Input:

2
2 2

Output:

8

Example 3

Input:

1
1

Output:

1

Example notes

Example 1: the six contiguous segments contribute 3, 6, 12, 1, 8 and 4 in order, for a total of 34. Example 2: the two single-element segments contribute 2 each, and the whole segment contributes 2×2=4, for a total of 8. Example 3: the only segment contributes 1.

Editorial

Loading solution…