QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 1024 MB Total points: 100 Hackable ✓

#18916. Laser Tower

Statistics

There is one tower at each of the points $x = 1, 2, \ldots, N$ on the horizontal axis. The height of the tower at position $x=i$ is $h_i$, and $h_1, h_2, \ldots, h_N$ is a permutation of the integers from $1$ to $N$.

From the top of each tower, that is, from the point $(i,h_i)$, a laser is fired either to the left or to the right with equal probability.

  • If the laser is fired to the left, it covers the rectangular region satisfying $0 \le x \le i$ and $0 \le y \le h_i$.
  • If the laser is fired to the right, it covers the rectangular region satisfying $i \le x \le N+1$ and $0 \le y \le h_i$.

The directions in which the lasers are fired from all towers are determined independently.

Let $S$ be the area of the union of the regions covered after lasers are fired from all towers. Find the expected value of $2^S$ modulo $998\,244\,353$.

Input

The first line contains the number of towers $N$. ($2 \le N \le 200\,000$)

The second line contains $N$ integers $h_1,h_2,\ldots,h_N$, separated by spaces, representing the heights of the towers. The given heights form a permutation in which each integer from $1$ to $N$ appears exactly once.

Output

Output the expected value of $2^S$ modulo $998\,244\,353$. The number $998\,244\,353$ is prime.

More specifically, the expected value is guaranteed to be a rational number. If it is written as an irreducible fraction $P/Q$, it can be shown that $Q \not\equiv 0 \pmod{998\,244\,353}$. You must output the value of

$$ P \times Q^{-1} \pmod{998\,244\,353}. $$

Examples

Example 1

Input

2
1 2

Output

16

Example 2

Input

3
1 2 3

Output

768

Example 3

Input

6
6 5 2 3 4 1

Output

448265500

Note

In Example 1, the entire space ranges from $x=0$ to $x=3$. For the four possible combinations of directions in which towers $1$ and $2$ fire their lasers, the area $S$ of the union of the covered regions and the value of $2^S$ are as follows. Each case occurs with probability $1/4$.

  • Left, left: The interval $0 \le x \le 2$ is covered up to height $2$. ($S=4$, $2^S=16$)
  • Left, right: The interval $0 \le x \le 1$ is covered up to height $1$, and the interval $2 \le x \le 3$ is covered up to height $2$. ($S=3$, $2^S=8$)
  • Right, left: The interval $0 \le x \le 2$ is covered up to height $2$, and the interval $2 \le x \le 3$ is covered up to height $1$. ($S=5$, $2^S=32$)
  • Right, right: The interval $1 \le x \le 2$ is covered up to height $1$, and the interval $2 \le x \le 3$ is covered up to height $2$. ($S=3$, $2^S=8$)

Therefore, the expected value of $2^S$ is

$$ \frac{16+8+32+8}{4}=16. $$

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.