Under construction

Still being built. Things may change

The Maximum After Erasing

Time limit1sMemory limit512 MB

Summary
Given a permutation, sum the maximum of the remaining elements over every way to delete two disjoint intervals, modulo 10^9+7.
Level

Medium7 of 10

Topics
Array, Stack, Prefix sum
Solved
No attempts yet

Problem

A permutation AA of length NN is given. For every tuple (i,j,k,l)(i, j, k, l) with 1≤i≤j<k≤l≤N1 \le i \le j < k \le l \le N, remove the interval [i,j][i, j] and the interval [k,l][k, l] from AA, then take the maximum of the remaining elements. Find the sum of these maxima over all valid tuples. The maximum of an empty array is assumed to be 00.

Input

The first line contains NN (3≤N≤105)(3 \le N \le 10^5).

The second line contains the NN integers of permutation AA, separated by spaces. (1≤Ai≤N(1 \le A_i \le N; all AiA_i are distinct.))

Output

Print the answer modulo 109+710^9+7.

Hint

The interval [s,e][s, e] of AA means As,As+1,⋯ ,AeA_s, A_{s+1}, \cdots, A_e.

Examples1

  1. Example 1

    Input
    4
    4 2 3 1
    
    Expected output
    35