Equal Maximums
Time limit1sMemory limit512 MB
Count quadruples of indices i<=j<k<=l where the maximum of a[i..j] equals the maximum of a[k..l], modulo 1e9+7, for n up to 100000.
- Level
Hard8 of 10
- Topics
- Array, Stack, Combinatorics, Implementation
- Solved
- No attempts yet
Problem
Sasha is preparing for programming contests, so he is studying data structures and related problems. One common problem he has noticed is the Range Maximum Query.
That problem is defined as follows. There is an array of integers: . One has to answer queries of the form "find the maximum value in the range from the -th element to the -th one", so the value to compute is .
Of course, this task is not difficult for Sasha, and soon he had a very fast program that answers such queries. Looking at the answers, he noticed that the answers for different queries are often the same.
Now Sasha wonders how many ways there are to choose a pair of non-overlapping ranges that have equal maximum elements.
Your task is to help Sasha. Find the number of quadruples , , , such that and . Since that number can be quite large, you have to output it modulo .
Input
The first line of input contains one integer , the length of Sasha's array (). The second line of input contains integers, the array elements. They are positive and do not exceed .
Output
Output the number of pairs of non-overlapping ranges with equal maximums, modulo .
Hint
Pairs of ranges from the first sample: