QOJ.ac

QOJ

Time Limit: 1.5 s Memory Limit: 512 MB Total points: 100

#1610. Sequence

Statistics

Kanan has an integer sequence $r_i$ of length $n$. She wants to construct an integer sequence $A$ of length $n$ that satisfies the following conditions:

  • $1 \leq A_i \leq r_i$.
  • For any $3 \leq i \leq n$, let $R$ be the minimum value among $A_1$ to $A_{i-2}$ that is greater than or equal to $A_{i-1}$, and let $L$ be the maximum value among $A_1$ to $A_{i-2}$ that is less than or equal to $A_{i-1}$. $A_i$ must satisfy $L \leq A_i \leq R$. If there is no value greater than or equal to $A_{i-1}$, then $R = +\infty$; if there is no value less than or equal to $A_{i-1}$, then $L = -\infty$.

Kanan wants to know how many different sequences satisfy these conditions. Two sequences $A$ and $B$ are different if and only if there exists at least one position $i$ such that $A_i \neq B_i$.

Input

The first line contains an integer $n$. The second line contains $n$ integers $r_i$.

Output

Output an integer representing the number of valid sequences. Since the answer may be very large, output it modulo $998244353$.

Examples

Input 1

3
2 2 2

Output 1

6

Note 1

The sequences satisfying the conditions are $[1, 1, 1], [1, 2, 1], [1, 2, 2], [2, 1, 1], [2, 1, 2], [2, 2, 2]$.

Subtasks

Test Case ID $n$ $r_i$
$1$ $\leq 7$ $\leq 7$
$2$
$3$ $\leq 50$ $\leq 10$
$4$
$5$ $\leq 16$
$6$
$7$ $\leq 50$
$8$
$9$ $\leq 150$
$10$

For all test cases, it is guaranteed that $n \geq 1$ and $r_i \geq 1$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.