#P4063. [JXOI2017] 数列

[JXOI2017] 数列

Description

Jiutiao Kelian (pinyin) has an integer sequence rir_i of length nn. She now wants to construct an integer sequence AA of length nn that satisfies the following:

  • 1Airi1 \le A_i \le r_i.

  • For any 3in3 \le i \le n, let RR be the minimum among A1A_1 through Ai2A_{i-2} that are greater than or equal to Ai1A_{i-1}, and let LL be the maximum among A1A_1 through Ai2A_{i-2} that are less than or equal to Ai1A_{i-1}. Then AiA_i must satisfy LAiRL \le A_i \le R. If there is no number greater than or equal to Ai1A_{i-1}, then R=+R = +\infty; if there is no number less than or equal to Ai1A_{i-1}, then L=L = -\infty.

Now she wants to know how many different sequences satisfy these conditions. Two sequences AA and BB are different if and only if there exists at least one position ii such that AiBiA_i \ne B_i.

Input Format

The first line contains an integer nn, and the second line contains nn integers rir_i.

Output Format

Output a single integer denoting the number of valid sequences. The answer can be large; output it modulo 998244353998244353.

3
2 2 2
6

Hint

For example, when n=3n = 3 and ri=2r_i = 2 for all ii, the valid sequences are $[1, 1, 1], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 2]$.

Constraints:

Test point ID nn rir_i
1,21, 2 n7n \le 7 ri7r_i \le 7
3,43, 4 n50n \le 50 ri10r_i \le 10
5,65, 6 ri16r_i \le 16
7,87, 8 ri50r_i \le 50
9,109, 10 ri150r_i \le 150

Translated by ChatGPT 5