n towers of heights 1, 2, …, n: all heights are different and each appears exactly once. An observer looks at the row from the left. A tower is visible if no taller tower stands anywhere to its left; the first tower is always visible.
For example, in the row [2, 1, 4, 3, 5] the towers of height 2, 4 and 5 are visible.
Count the lineups in which the observer sees exactly visible towers. The answer can be large, so return it modulo 10⁹ + 7.n=4, visible=3 → 6
Six lineups work, for example [1, 2, 4, 3]: 1, 2 and 4 are visible, while 3 is hidden behind 4.
n=3, visible=1 → 2
Only one tower is visible, so the tallest one must stand first: [3, 1, 2] and [3, 2, 1].
n=6, visible=6 → 1
Every tower is visible only when they stand in increasing order.
n ≤ 1000visible ≤ nn=4, visible=3
6
Run checks the first 3 tests in your browser. Submit checks all 60 tests on the server, hidden ones included.