THEATRE - Movie Theatre Madness


A group of friends have gone to watch the first day first show of an awesome new movie. However, since they did not book the tickets well in advance, they have ended up with crazy seats. To be more specific, rather than getting seats such that all the friends are seated in the same row, they have ended up with seats such that all of them are seated in the same column! Now this is very inconvenient since they won't be able to chat with each other during the movie or have any kind of fun, but they are okay with this since they'd rather watch the movie like this, than not watch the movie at all.

But there is another problem apart from this. Now we know that all the friends are seated in a single column, one behind the other. Since all of them reached the theatre just in time for the movie, they rushed and occupied the first of the booked seats that they could find. However all the friends have different heights, and due to the lack of planning, there is no guarantee that a shorter person is always seated in front of a taller person. But this would mean that the shorter person would struggle to see the screen throughout the movie!

But the movie has started and it's too late now to do anything.

What you need to do is the following: For every person, find the height of the closest person seated in front of him/her who is blocking his/her view (that is, the person closest in front with a greater height). If no such person exists, take this height as 1. Print the product of all such values modulo 1000000007.

Input

On the first line you have a single integer N (2 <= N <= 105), the total number of friends.

This is followed by N space separated integers a1, a2, ... aN, which correspond to the height of the people from back to front. That is, a1 is the height of the person seated on the last row, a2 is the height of the person seated on the second last row (just in front of a1) and so on, up to aN which is the height of the person seated right at the very front (1 <= ai <= 109). Note that all these integers are distinct.

Output

On a single line, output the result. (For every person, find the height of the closest blocking person. This value is 1 if no such person exists. Print the product of all these values modulo 1000000007).

NOTE: By closest blocking person to ai we mean find aj, such that aj > ai, j > i, and (j-i) is minimum. (Please look at sample test cases for further clarity.)

Example

Input #1:
5
5 2 1 4 3
 
Output #1:
16
Input #2:
5
9 8 3 5 7

Output #2:
35
Input #3:
10
30 10 50 70 11 60 20 80 31 12

Output #3:
999962375

Explanation

Input #1:

  • blockingHeight(5) = 1 (since 5 is the tallest, no one is blocking him)
  • blockingHeight(2) = 4
  • blockingHeight(1) = 4 (although 3 is also taller that 1, 4 is closer to 1)
  • blockingHeight(4) = 1 (No one blocking 4)
  • blockingHeight(3) = 1 (No one blocking 3)
  • Answer=1*4*4*1*1=16

hide comments
sepehragz1382: 2019-07-18 08:10:35

12
8 4 6 3 7 9 12 1 13 17 16 19
answer --> 385136432

sepehragz1382: 2019-07-18 08:06:52

9
6 3 5 5 2 7 4 2 5
300125

Last edit: 2019-07-18 08:11:04
rishabh_1997: 2016-07-09 23:44:50

AC in one attempt. 0.08s
Learnt many good things after working for 2 hours.
Use stack wisely. :-)

Riddhi: 2016-05-26 15:40:19

try this-
10
8 6 7 56 12 48 23 47 23 11
op- 49523712
and
10
9 8 2 3 6 1 7 4 5 10
op-44100000

bunnycoder: 2016-01-14 19:18:00

something new gud question

Vipul Srivastava: 2015-03-18 19:27:24

Leant something new!! Thanks setter.

zicowa: 2014-12-28 14:50:20

I know that this problem can be solved using stack with O(N) time complexity even it can be solved without using stack ..

i have O(Nlog(N)) accepted using segment tree but as i can see time limit time for this problem .. is 2 sec ... My O(Nlog^2(N)) is producing TLE why this so ..

Noob: 2014-12-03 08:25:41

Nice question :)

Ruchir Sharma: 2014-11-12 15:17:16

AC in frst attempt..!! simple O(n) algo wud work.. (Y)

Kid Algorist: 2014-10-25 13:35:19

DP like approach gives 0.27s AC.
5
100000 10000 1000 100 1000000
o/p
49000000


Added by:Gowri Sundaram
Date:2013-03-16
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM32-GCC MAWK BC C-CLANG NCSHARP CPP14 CPP14-CLANG COBOL COFFEE D-CLANG D-DMD DART ELIXIR FANTOM FORTH GOSU GRV JS-MONKEY JULIA KTLN NIM OBJC OBJC-CLANG OCT PICO PROLOG PYPY PYPY3 R RACKET RUST CHICKEN SQLITE SWIFT UNLAMBDA VB.NET