The Maximum After Erasing
Time limit1sMemory limit512 MB
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 of length is given. For every tuple with , remove the interval and the interval from , 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 .
Input
The first line contains .
The second line contains the integers of permutation , separated by spaces. ; all are distinct.
Output
Print the answer modulo .
Hint
The interval of means .