Equal Maximums

아직 제출이 없습니다시간 제한1초메모리 제한512 MB

문제

Sasha is preparing for programming contests, so he is studying data structures and corresponding problems. One common problem he has noticed is Range Maximum Query. 

That problem is defined as follows. There is an array aa of nn integers: a_1,a_2,,a_na\_1, a\_2, \ldots, a\_n. One has to answer queries "find the maximum value in a range from the ii-th element to the jj-th one", so the problem is to calculate maxa_i,a_i+1,,a_j\max \\{a\_i, a\_{i+1}, \ldots, a\_j\\}.

Of course, this task is not difficult for Sasha, and soon there was a very fast program that answers such queries. Looking at the answers, he has noticed that the answers for different queries are often the same. 

Now Sasha is wondering, how many ways are there to choose a pair of non-overlapping ranges that have equal maximum elements.

Your task is to help Sasha. Find the number of quadruples ii, jj, kk, ll, such that 1ij<kln1 \le i \le j < k \le l \le n and maxa_i,a_i+1,,a_j=maxa_k,a_k+1,,a_l\max\\{a\_i, a\_{i+1}, \ldots, a\_j\\} = \max\\{a\_k, a\_{k+1}, \ldots, a\_l\\}. Since that number can be quite large, you have to output it modulo 1,000,000,0071\\,000\\,000\\,007.

입력

The first line of input contains one integer nn: the length of Sasha's array (2n100,0002 \le n \le 100\\,000). The second line of input contains nn integers: array elements (they are positive and don't exceed 10910^9).

출력

Output the number of pairs of non-overlapping ranges with equal maximums. Print it modulo 109+710^9 + 7.

힌트

Pairs of ranges from the first sample:

\[3],\[3],4,4,3,2\[\mathbf{3}], \[\mathbf{3}], 4, 4, 3, 2i=1i=1j=1j=1k=2k=2l=2l=2
\[3],3,4,4,\[3],2\[\mathbf{3}], 3, 4, 4, \[\mathbf{3}], 2i=1i=1j=1j=1k=5k=5l=5l=5
\[3],3,4,4,\[3,2\[\mathbf{3}], 3, 4, 4, \[\mathbf{3}, 2]i=1i=1j=1j=1k=5k=5l=6l=6
\[3,3],4,4,\[3],2\[\mathbf{3}, \mathbf{3}], 4, 4, \[\mathbf{3}], 2i=1i=1j=2j=2k=5k=5l=5l=5
\[3,3],4,4,\[3,2]\[\mathbf{3}, \mathbf{3}], 4, 4, \[\mathbf{3}, 2]i=1i=1j=2j=2k=5k=5l=6l=6
3,\[3],4,4,\[3],23, \[\mathbf{3}], 4, 4, \[\mathbf{3}], 2i=2i=2j=2j=2k=5k=5l=5l=5
3,\[3],4,4,\[3,2]3, \[\mathbf{3}], 4, 4, \[\mathbf{3}, 2]i=2i=2j=2j=2k=5k=5l=6l=6
\[3,3,4],\[4],3,2\[3, 3, \mathbf{4}], \[\mathbf{4}], 3, 2i=1i=1j=3j=3k=4k=4l=4l=4
\[3,3,4],\[4,3],2\[3, 3, \mathbf{4}], \[\mathbf{4}, 3], 2i=1i=1j=3j=3k=4k=4l=5l=5
\[3,3,4],\[4,3,2]\[3, 3, \mathbf{4}], \[\mathbf{4}, 3, 2]i=1i=1j=3j=3k=4k=4l=6l=6
3,\[3,4],\[4],3,23, \[3, \mathbf{4}], \[\mathbf{4}], 3, 2i=2i=2j=3j=3k=4k=4l=4l=4
3,\[3,4],\[4,3],23, \[3, \mathbf{4}], \[\mathbf{4}, 3], 2i=2i=2j=3j=3k=4k=4l=5l=5
3,\[3,4],\[4,3,2]3, \[3, \mathbf{4}], \[\mathbf{4}, 3, 2]i=2i=2j=3j=3k=4k=4l=6l=6
3,3,\[4],\[4],3,23, 3, \[\mathbf{4}], \[\mathbf{4}], 3, 2i=3i=3j=3j=3k=4k=4l=4l=4
3,3,\[4],\[4,3],23, 3, \[\mathbf{4}], \[\mathbf{4}, 3], 2i=3i=3j=3j=3k=4k=4l=5l=5
3,3,\[4],\[4,3,2]3, 3, \[\mathbf{4}], \[\mathbf{4}, 3, 2]i=3i=3j=3j=3k=4k=4l=6l=6