atcoder#ARC108E. [ARC108E] Random IS

[ARC108E] Random IS

Score : 700700 points

Problem Statement

There are NN isu - chairs in Japanese - arranged from left to right. The ii-th chair from the left has the ID number aia_i. Here, it is guaranteed that aia_i are distinct.

Snuke has decided to mark some of the chairs and throw away the rest. Initially, no chair is marked. We call a choice of marked chairs good when the IDs of the marked chairs are monotonically increasing from left to right.

Snuke has decided to do the following procedure to mark chairs:

  1. We say a chair xx to be nice if (and only if) the choice of marked chairs is still good when xx gets marked. Let kk be the current number of nice chairs.
  2. If k=0k=0, remove the unmarked chairs and terminate the procedure. Otherwise, choose one from the kk nice chairs with equal probability, mark it, and go back to Step 1.

It can be proved that the expected value of the number of chairs that remain at the end of the procedure is a rational number. Let this value be P/QP/Q, an irreducible fraction. Additionally, let M=109+7M=10^{9}+7. Then, we can prove that there uniquely exists an integer RR between 00 and M1M-1 (inclusive) such that PQ×R(modM)P \equiv Q \times R \pmod{M}, and that value equals P×Q1(modM)P \times Q^{-1} \pmod{M}, where Q1Q^{-1} is the modular multiplicative inverse of QQ. Find RR.

Constraints

  • All values in input are integers.
  • 1N20001 \leq N \leq 2000
  • 1aiN1 \leq a_i \leq N
  • aia_i are distinct.

Input

Input is given from Standard Input in the following format:

NN

a1a_1 a2a_2 \cdots aNa_N

Output

Print RR.

3
3 1 2
666666673
  • If Chair 11 (the chair with the ID number 11) gets marked first, two chairs will remain at the end: Chair 11 and 22.
  • If Chair 22 gets marked first, two chairs will remain at the end: Chair 11 and 22.
  • If Chair 33 gets marked first, one chair will remain at the end: Chair 33.
  • Since chairs are chosen with equal probability, the expected value of the number of chairs at the end is 53\frac{5}{3}. We have 53×666666673(modM)5 \equiv 3 \times 666666673 \pmod{M}, so R=666666673R=666666673.
30
26 16 28 30 23 11 29 18 22 15 20 13 27 9 21 7 5 25 4 19 8 3 1 24 10 14 17 12 2 6
297703424