#FT0000103. Sereja và các dãy con (Sereja and Subsequences)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 positive integers .
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 , he writes every positive-integer sequence of the same length satisfying for every . If the same can be produced from several different sequences, it is written and counted once for each such .
Find the total number of sequences written in this second step, counting repeated writings, modulo .
Input
- The first line contains integer , the original sequence length.
- The next line contains positive integers in order.
Output
Print one integer: the total number of sequences written in the second step, modulo .
Subtasks
- Subtask 1 (20 points): ; for every .
- Subtask 2 (30 points): ; for every .
- Subtask 3 (50 points): ; for every .
Examples
Example 1
Input
3
1 2 2
Output
13
Explanation
For , the distinct nondecreasing subsequences are , , , , and . The values appear only once in this list even though there are two possible choices of the index containing . Each listed sequence contributes a number of positive sequences equal to the product of its entries: , , , , and . The sum is , so print 13.
Example 2
Input
1
42
Output
42
Explanation
The only nonempty nondecreasing subsequence is . For this sequence, precisely the values are allowed. There are sequences of length one, so print 42.