#ARC104E. [ARC104E] Random LIS

[ARC104E] Random LIS

Score : 700700 points

Problem Statement

Given is an integer sequence of length NN: A1,A2,,ANA_1, A_2, \cdots, A_N.

An integer sequence XX, which is also of length NN, will be chosen randomly by independently choosing XiX_i from a uniform distribution on the integers 1,2,,Ai1, 2, \ldots, A_i for each i(1iN)i (1 \leq i \leq N).

Compute the expected value of the length of the longest increasing subsequence of this sequence XX, modulo 10000000071000000007.

More formally, under the constraints of the problem, we can prove that the expected value can be represented as a rational number, that is, an irreducible fraction PQ\frac{P}{Q}, and there uniquely exists an integer RR such that R×QP(mod1000000007)R \times Q \equiv P \pmod {1000000007} and 0R<10000000070 \leq R < 1000000007, so print this integer RR.

Notes

A subsequence of a sequence XX is a sequence obtained by extracting some of the elements of XX and arrange them without changing the order. The longest increasing subsequence of a sequence XX is the sequence of the greatest length among the strictly increasing subsequences of XX.

Constraints

  • 1N61 \leq N \leq 6
  • 1Ai1091 \leq A_i \leq 10^9
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \cdots ANA_N

Output

Print the expected value modulo 10000000071000000007.

3
1 2 3
2

XX becomes one of the following, with probability 1/61/6 for each:

  • X=(1,1,1)X = (1,1,1), for which the length of the longest increasing subsequence is 11;
  • X=(1,1,2)X = (1,1,2), for which the length of the longest increasing subsequence is 22;
  • X=(1,1,3)X = (1,1,3), for which the length of the longest increasing subsequence is 22;
  • X=(1,2,1)X = (1,2,1), for which the length of the longest increasing subsequence is 22;
  • X=(1,2,2)X = (1,2,2), for which the length of the longest increasing subsequence is 22;
  • X=(1,2,3)X = (1,2,3), for which the length of the longest increasing subsequence is 33.

Thus, the expected value of the length of the longest increasing subsequence is $(1 + 2 + 2 + 2 + 2 + 3) / 6 \equiv 2 \pmod {1000000007}$.

3
2 1 2
500000005

XX becomes one of the following, with probability 1/41/4 for each:

  • X=(1,1,1)X = (1,1,1), for which the length of the longest increasing subsequence is 11;
  • X=(1,1,2)X = (1,1,2), for which the length of the longest increasing subsequence is 22;
  • X=(2,1,1)X = (2,1,1), for which the length of the longest increasing subsequence is 11;
  • X=(2,1,2)X = (2,1,2), for which the length of the longest increasing subsequence is 22.

Thus, the expected value of the length of the longest increasing subsequence is (1+2+1+2)/4=3/2(1 + 2 + 1 + 2) / 4 = 3 / 2. Since 2×5000000053(mod1000000007)2 \times 500000005 \equiv 3 \pmod {1000000007}, we should print 500000005500000005.

6
8 6 5 10 9 14
959563502