Real Mountains

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

문제

Thanks to your help with cropping her picture, Rebecca's scenic photo is now featured on the front cover of the newest issue of her magazine. However, it seems that some of her readers still aren't pleased with the picture. In particular, they seem to believe that the mountain in the picture is fake!

For simplicity, we can describe the picture as a sequence of NN columns of pixels. In the ii-th column, the first h_ih\_i pixels from the bottom are of mountains. Her readers will only believe that the picture contains a real mountain if it contains a single (possibly wide) peak. That is, if there exists some index pp with 1pN1 \le p \le N such that h_1h_2h_ph_N1h_Nh\_1 \le h\_2 \le \ldots \le h\_p \ge \ldots \ge h\_{N-1} \ge h\_{N}.

Luckily, Rebecca can still pay her editors to modify the picture and reprint the magazine. Unfortunately for her though, the editors have a very peculiar pricing scheme for their work. The only way Rebecca can edit the picture is by sending emails to her editors containing the integers (i,j,k)(i, j, k) such that 1i<j<kN1 \le i < j < k \le N and h_i>h_j<h_kh\_i> h\_j< h\_k. The editors will then add an extra pixel of mountains in the jj-th column (i.e. increment h_jh\_j by 11) for a cost of h_i+h_j+h_kh\_i+ h\_j+ h\_k cents. Note that the change in h_jh\_j may affect the costs of future edits.

To please her readers, Rebecca would like to edit the picture so that they believe it contains a real mountain. Can you tell her the minimum cost required to do so?

입력

The first line of input contains an integer NN.

The second line of input contains NN space-separated integers h_1,h_2,,h_Nh\_1, h\_2, \ldots , h\_N.

출력

Output the remainder of TT divided by the prime number 106+310^6+ 3 where TT is the minimum cost (in cents) that Rebecca would need to incur in order to please her readers.

제한

  • 3N1063 \le N \le 10^6
  • 1h_i1091 \le h\_i \le 10^9

힌트

Rebecca can send two emails, the first containing the integers (2,6,7)(2, 6, 7) and the second containing the integers (1,2,5)(1, 2, 5). The first email costs 55 cents and increases h_6h\_6 by 11, while the second email costs 99 cents and increases h_2h\_2 by 11.

The h_ih\_i values in the final picture will be \[3,3,4,5,4,2,2,1]\[3, 3, 4, 5, 4, 2, 2, 1]. Her readers will believe this final picture contains a real mountain.