#P1876D. Lexichromatography

    ID: 10101 Type: RemoteJudge 2000ms 512MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>combinatoricsdfs and similardsugraphstwo pointers

Lexichromatography

Description

Pak Chanek loves his faculty, the Faculty of Computer Science, University of Indonesia (Fasilkom). He wants to play with the colours of the faculty's logo, blue and red.

There is an array aa consisting of nn elements, element ii has a value of aia_i. Pak Chanek wants to colour each element in the array blue or red such that these following conditions are satisfied:

  • If all blue elements are formed into a subsequence^\dagger and so are all the red elements, the blue subsequence is strictly less than the red subsequence lexicographically^\ddagger.
  • Array aa does not have any subarray that is imbalanced. A subarray is imbalanced if and only if there is a value kk such that the absolute difference between the number of blue elements with value kk and the number of red elements with value kk in this subarray is 22 or more.
  • Note that it is possible to colour every element of the array the same colour.

How many different colourings satisfy all those conditions? Since the answer can be very big, print the answer modulo 998244353998\,244\,353. Two colourings are different if and only if there is at least one element that is blue in one colouring, but red in the other.

^\dagger A subsequence of an array is a sequence that can be obtained from the array by deleting some elements (possibly none), without changing the order of the remaining elements.

^\ddagger Let pp and qq be two different sequences. Sequence pp is said to be lexicographically less than sequence qq if and only if pp is a prefix of qq or there is an index ii such that pj=qjp_j=q_j holds for every 1j<i1\leq j<i, and pi<qip_i<q_i. In particular, an empty sequence is always lexicographically less than any non-empty sequence.

The first line contains a single integer nn (1n21051 \leq n \leq 2\cdot10^5) — the size of array aa.

The second line contains nn integers a1,a2,a3,,ana_1,a_2,a_3,\ldots,a_n (1ai21051\leq a_i\leq2\cdot10^5).

An integer representing the number of different colourings that satisfy all of the problem's conditions, modulo 998244353998\,244\,353.

Input

The first line contains a single integer nn (1n21051 \leq n \leq 2\cdot10^5) — the size of array aa.

The second line contains nn integers a1,a2,a3,,ana_1,a_2,a_3,\ldots,a_n (1ai21051\leq a_i\leq2\cdot10^5).

Output

An integer representing the number of different colourings that satisfy all of the problem's conditions, modulo 998244353998\,244\,353.

Sample Input 1

8
1 3 1 2 3 2 3 3

Sample Output 1

3

Sample Input 2

1
265

Sample Output 2

1

Note

In the first example, the 33 ways for colouring all elements from index 11 to index 88 are:

  • Blue, red, red, blue, blue, red, red, blue.
  • Blue, red, red, red, blue, blue, red, blue.
  • Red, red, blue, blue, blue, red, red, blue.

As an example, if we colour the elements from index 11 to index 88 to be red, red, blue, red, red, blue, blue, blue, it is not valid colouring, because for subarray a[2..6]a[2..6], there are 00 blue elements with value 33 and 22 red elements with value 33, making subarray a[2..6]a[2..6] an imbalanced subarray.