PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000103. Sereja và các dãy con (Sereja and Subsequences)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sereja và các dãy con (Sereja and Subsequences)

Sereja and Subsequences

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Sereja has a sequence of nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n.

First, he lists all distinct nonempty nondecreasing subsequences, where distinctness is by their sequence of values. A subsequence is formed by deleting some positions without changing the order of the remaining positions. Different index selections producing the same values contribute only one subsequence to this list.

For each listed sequence y=(y1,…,yr)y=(y_1,\ldots,y_r), he writes every positive-integer sequence x=(x1,…,xr)x=(x_1,\ldots,x_r) of the same length satisfying 1≤xt≤yt1\le x_t\le y_t for every 1≤t≤r1\le t\le r. If the same xx can be produced from several different yy sequences, it is written and counted once for each such yy.

Find the total number of sequences written in this second step, counting repeated writings, modulo 109+710^9+7.

Input

  • The first line contains integer nn, the original sequence length.
  • The next line contains nn positive integers a1,a2,…,ana_1,a_2,\ldots,a_n in order.

Output

Print one integer: the total number of sequences written in the second step, modulo 109+710^9+7.

Subtasks

  • Subtask 1 (20 points): 1≤n≤161\le n\le 16; 1≤ai≤1061\le a_i\le 10^6 for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤20001\le n\le 2000; 1≤ai≤1061\le a_i\le 10^6 for every 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤1000001\le n\le 100000; 1≤ai≤1061\le a_i\le 10^6 for every 1≤i≤n1\le i\le n.

Examples

Example 1

Input

3
1 2 2

Output

13

Explanation

For (1,2,2)(1,2,2), the distinct nondecreasing subsequences are (1)(1), (2)(2), (1,2)(1,2), (2,2)(2,2), and (1,2,2)(1,2,2). The values (1,2)(1,2) appear only once in this list even though there are two possible choices of the index containing 22. Each listed sequence yy contributes a number of positive sequences xx equal to the product of its entries: 11, 22, 1⋅2=21\cdot2=2, 2⋅2=42\cdot2=4, and 1⋅2⋅2=41\cdot2\cdot2=4. The sum is 1+2+2+4+4=131+2+2+4+4=13, so print 13.

Example 2

Input

1
42

Output

42

Explanation

The only nonempty nondecreasing subsequence is (42)(42). For this sequence, precisely the values x1=1,2,…,42x_1=1,2,\ldots,42 are allowed. There are 4242 sequences of length one, so print 42.