#P4063. [JXOI2017] 数列

[JXOI2017] 数列

Problem 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:

  • 1≤Ai≤ri1 \le A_i \le r_i.

  • For any 3≤i≤n3 \le i \le n, let RR be the minimum among A1A_1 through Ai−2A_{i-2} that are greater than or equal to Ai−1A_{i-1}, and let LL be the maximum among A1A_1 through Ai−2A_{i-2} that are less than or equal to Ai−1A_{i-1}. Then AiA_i must satisfy L≤Ai≤RL \le A_i \le R. If there is no number greater than or equal to Ai−1A_{i-1}, then R=+∞R = +\infty; if there is no number less than or equal to Ai−1A_{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 Ai≠BiA_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 n≤7n \le 7 ri≤7r_i \le 7
3,43, 4 n≤50n \le 50 ri≤10r_i \le 10
5,65, 6 ri≤16r_i \le 16
7,87, 8 ri≤50r_i \le 50
9,109, 10 ri≤150r_i \le 150

Translated by ChatGPT 5