#AGC058B. [AGC058B] Adjacent Chmax

[AGC058B] Adjacent Chmax

Score : 700700 points

Problem Statement

You are given a permutation P=(P1,P2,,PN)P=(P_1,P_2,\cdots,P_N) of (1,2,,N)(1,2,\cdots,N).

You may perform the following operation zero or more times.

  • Choose an integer ii (1iN11 \leq i \leq N-1). Let v=max(Pi,Pi+1)v=\max(P_i,P_{i+1}) and replace the values of PiP_i and Pi+1P_{i+1} with vv.

How many sequences are there that PP can be after your operations? Find the count modulo 998244353998244353.

Constraints

  • 2N50002 \leq N \leq 5000
  • (P1,P2,,PN)(P_1,P_2,\cdots,P_N) is a permutation of (1,2,,N)(1,2,\cdots,N).
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

P1P_1 P2P_2 \cdots PNP_N

Output

Print the answer.

3
1 3 2
4

The four sequences that PP can become are (1,3,2)(1,3,2), (3,3,2)(3,3,2), (1,3,3)(1,3,3), and (3,3,3)(3,3,3).

4
2 1 3 4
11
10
4 9 6 3 8 10 1 2 7 5
855