Lexichromatography
Contest: Codeforces Round 902 (Div. 1, based on COMPFEST 15 - Final Round)
Difficulty rating: 2500
Tags: combinatorics, dfs and similar, dsu, graphs, two pointers

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 $$$a$$$ consisting of $$$n$$$ elements, element $$$i$$$ has a value of $$$a_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 $$$a$$$ does not have any subarray that is imbalanced. A subarray is imbalanced if and only if there is a value $$$k$$$ such that the absolute difference between the number of blue elements with value $$$k$$$ and the number of red elements with value $$$k$$$ in this subarray is $$$2$$$ 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 $$$998\,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 $$$p$$$ and $$$q$$$ be two different sequences. Sequence $$$p$$$ is said to be lexicographically less than sequence $$$q$$$ if and only if $$$p$$$ is a prefix of $$$q$$$ or there is an index $$$i$$$ such that $$$p_j=q_j$$$ holds for every $$$1\leq j<i$$$, and $$$p_i<q_i$$$. In particular, an empty sequence is always lexicographically less than any non-empty sequence.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 2\cdot10^5$$$) — the size of array $$$a$$$.

The second line contains $$$n$$$ integers $$$a_1,a_2,a_3,\ldots,a_n$$$ ($$$1\leq a_i\leq2\cdot10^5$$$).

Output format:
An integer representing the number of different colourings that satisfy all of the problem's conditions, modulo $$$998\,244\,353$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first example, the $$$3$$$ ways for colouring all elements from index $$$1$$$ to index $$$8$$$ 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 $$$1$$$ to index $$$8$$$ to be red, red, blue, red, red, blue, blue, blue, it is not valid colouring, because for subarray $$$a[2..6]$$$, there are $$$0$$$ blue elements with value $$$3$$$ and $$$2$$$ red elements with value $$$3$$$, making subarray $$$a[2..6]$$$ an imbalanced subarray.

Example input:
8
1 3 1 2 3 2 3 3

Example output:
3

Example input:
1
265

Example output:
1

Editorial:
There are two conditions in the problem. The lexicography condition and the imbalanced subarrays condition.
Let's look at the imbalanced subarrays condition. Consider some value $$$k$$$ and consider all elements in $$$a$$$ with a value of $$$k$$$. In order to satisfy this condition, those elements must be coloured blue and red alternatingly from left to right. So for each value $$$k$$$ that is present in $$$a$$$, there are $$$2$$$ ways to colour its elements, either starting from blue or starting from red. This means, if we ignore the lexicography condition, the number of colourings is $$$2^c$$$ with $$$c$$$ representing the number of different values in $$$a$$$.
From now on, we are only considering the colourings that satisfy the imbalanced subarray condition.
Define the following:
• $$$\text{less}$$$: the number of colourings with the blue subsequence less than the red subsequence lexicographically.
• $$$\text{equal}$$$: the number of colourings with the blue subsequence equal to the red subsequence.
• $$$\text{more}$$$: the number of colourings with the blue subsequence bigger than the red subsequence lexicographically.
We can get that $$$\text{less}+\text{equal}+\text{more}=2^c$$$. Furthermore, we can get that $$$\text{less}=\text{more}$$$ since we can always flip the colouring to get the opposite one. Therefore, $$$\text{less}=\frac{2^c-\text{equal}}{2}$$$.
Now, let's calculate the value of $$$\text{equal}$$$. Iterate the array from left to right while simultaneously maintaining both the blue and red subsequences. During the iteration, we want both subsequences to be equal. For the first element, we can colour it either red or blue, and put the value of that element into its corresponding subsequence. We iterate each element but we stop the next time both subsequences have the same length. So if we colour the first element blue, then the blue subsequence will be longer than the red one throughout this process, until both subsequences have equal lengths. For each of the next elements, there are a few cases:
• If we have not found an element with this value before, we must put the element in the currently longer subsequence. (If we put it in the shorter subsequence it will not match with the corresponding element in the longer subsequence.
• If we have found an element with this value before, it must be the opposite colour than the previous one. This means we must put it in the shorter subsequence. If the corresponding element in the longer subsequence does not match, then $$$\text{equal}=0$$$. Otherwise, we can continue the process.
Once both subsequences have equal lengths, let's say we ignore all of the previous elements and the currently built subsequences and assume we continue iterating the array while forgetting about all things we did before. We cannot actually do this to calculate $$$\text{equal}$$$ properly, but we will get to that later. Notice that continuing the iteration now is just like how we start in the beginning. We can choose the colour of the first element and every other element after that is forced until both subsequences have the same length. We continue doing this repeatedly until the end of $$$a$$$.
If at the end of $$$a$$$, the lengths of both subsequences are different or we hit the $$$\text{equal}=0$$$ case at least once, then $$$\text{equal}=0$$$.
Otherwise, from our assumption above, we can deduce that each time we add an element when both subsequences have equal lengths, we can freely choose the colour of that element. We can split $$$a$$$ into several sections where the sections are separated by the times both subsequences have the same length. Using our assumption above, the number of colourings is $$$2^d$$$ with $$$d$$$ being the number of sections, because there are $$$2$$$ starting colours we can choose for each section.
However, as we have said before, when starting a new section, we cannot completely ignore the previous sections. Since the colouring of the elements with the same value must be alternating, two sections that have at least one common value must have the same starting colour. This means we can construct a graph of sections and connect the sections that have a common value with edges. Then, the true value of $$$\text{equal}$$$ is $$$2^{d'}$$$ with $$$d'$$$ being the number of connected components.
Time complexity: $$$O(n)$$$