Long Colorful Strip
Contest: Codeforces Global Round 4
Difficulty rating: 2600
Tags: dp

This is the second subtask of problem F. The only differences between this and the first subtask are the constraints on the value of $$$m$$$ and the time limit. It is sufficient to solve this subtask in order to hack it, but you need to solve both subtasks in order to hack the first one.

There are $$$n+1$$$ distinct colours in the universe, numbered $$$0$$$ through $$$n$$$. There is a strip of paper $$$m$$$ centimetres long initially painted with colour $$$0$$$.

Alice took a brush and painted the strip using the following process. For each $$$i$$$ from $$$1$$$ to $$$n$$$, in this order, she picks two integers $$$0 \leq a_i < b_i \leq m$$$, such that the segment $$$[a_i, b_i]$$$ is currently painted with a single colour, and repaints it with colour $$$i$$$.

Alice chose the segments in such a way that each centimetre is now painted in some colour other than $$$0$$$. Formally, the segment $$$[i-1, i]$$$ is painted with colour $$$c_i$$$ ($$$c_i \neq 0$$$). Every colour other than $$$0$$$ is visible on the strip.

Count the number of different pairs of sequences $$$\{a_i\}_{i=1}^n$$$, $$$\{b_i\}_{i=1}^n$$$ that result in this configuration.

Since this number may be large, output it modulo $$$998244353$$$.

Input format:
The first line contains a two integers $$$n$$$, $$$m$$$ ($$$1 \leq n \leq 500$$$, $$$n \leq m \leq 10^6$$$) — the number of colours excluding the colour $$$0$$$ and the length of the paper, respectively.

The second line contains $$$m$$$ space separated integers $$$c_1, c_2, \ldots, c_m$$$ ($$$1 \leq c_i \leq n$$$) — the colour visible on the segment $$$[i-1, i]$$$ after the process ends. It is guaranteed that for all $$$j$$$ between $$$1$$$ and $$$n$$$ there is an index $$$k$$$ such that $$$c_k = j$$$.

Output format:
Output a single integer — the number of ways Alice can perform the painting, modulo $$$998244353$$$.

Constraints:
Time limit: 6.0 s
Memory limit: 256.0 MB

Note:
In the first example, there are $$$5$$$ ways, all depicted in the figure below. Here, $$$0$$$ is white, $$$1$$$ is red, $$$2$$$ is green and $$$3$$$ is blue.

Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour $$$2$$$.

In the second example, Alice must first paint segment 0 3 with colour $$$1$$$ and then segment 1 2 with colour $$$2$$$.

Example input:
3 3
1 2 3

Example output:
5

Example input:
2 3
1 2 1

Example output:
1

Editorial:
There are several additional observations we need compared to previous subtask.
First, note that if we ever colour a pair of positions with different colours, they will forever stay coloured with different colours. In other words, if two positions have the same colour in the final strip, the set of colours they were coloured with is the same. As a result, we can compress the input by removing consecutive equal values.
Next, we look at the number of changes in the strip. A change is a position $$$i$$$ such that $$$c_i \neq c_{i+1}$$$. Initially, there are $$$0$$$ changes, and each operation adds at most $$$2$$$ changes. As a result, if the length of the input after the compression is more than $$$2n$$$, we can immediately print $$$0$$$.
Now we can use the DP as in previous tasks, but there is one more complication to resolve. Consider the third sample:
The DP suggests that $$$DP[0][2] = DP[0][0] * DP[2][2] = 1$$$. The reason why we get a wrong answer is that we treat the intervals $$$[0,0]$$$ and $$$[2,2]$$$ as independent, but in reality they are not.
To fix this, instead of finding a single value of $$$I[c]$$$, we set $$$I'[c]$$$ to be all positions (in the whole array) containing colour $$$c$$$. We can then pick $$$l \leq a \leq \min(I'[c])$$$ and $$$\max(I'[c]) \leq b \leq r$$$. For this to be possible, all occurrences of the colour $$$c$$$ must be within interval $$$[l,r]$$$, otherwise $$$D[l][r] = 0$$$. The occurrences of colour $$$c$$$ and the indices $$$a$$$ and $$$b$$$ now can split the interval $$$[l,r]$$$ into more than $$$4$$$ segments, but this does not affect the complexity.
Final complexity is $$$\mathcal O(n^3 + m)$$$.
Bonus (courtesy of [user:Um_nik]): Can you solve it faster?