I admit that AGC061C is too hard for me, and I don't even understand its official editorial. Today I see a Luogu blog with a very clear idea on this problem. I learn a lot from it, and I would like to share it to you. The writer of this blog is possibly Little09, but I am not quite sure.
Part1: Problem Statement and Constraints
There are $$$N$$$ customers named $$$1$$$, ..., $$$N$$$ visiting a shop. Customer $$$i$$$ arrives at time $$$A_i$$$ and leaves at time $$$B_i$$$ The queue order is first in first out, so $$$A_i$$$ and $$$B_i$$$ are both increasing. Additionally, all $$$A_i$$$ and $$$B_i$$$ are pairwise distinct.
At the entrance, there's a list of visitors to put their names in. Each customer will write down their name next in the list exactly once, either when they arrive or when they leave. How many different orders of names can there be in the end? Find the count modulo $$$998244353$$$.
Constraints:
$$$\cdot 1 \leq N \leq 500000$$$.
$$$\cdot 1 \leq A_i \leq B_i \leq 2N$$$.
$$$\cdot A_i < A_{i+1}\,(1 \leq i \leq N-1)$$$.
$$$\cdot B_i < B_{i+1}\,(1 \leq i \leq N-1)$$$.
$$$\cdot A_i \neq B_j \,(i \neq j)$$$
$$$\cdot \text{All values in the input are integers}$$$.
Part2: Idea in the blog
Consider two dynamic programming tables named $$$f$$$ and $$$g$$$ ($$$dp$$$ in the original blog). The blog computes $$$f,\,g$$$ in the reverse order. Formally,
$$$f(i) (1 \leq i \leq n)$$$: The number of permutations when we have already considered persons $$$i,\,i+1,\,...,\,n$$$.
$$$g(i) (1 \leq i \leq n)$$$: The number of permutations when we have already considered persons $$$i,\,i+1,\,...,\,n$$$, and the person $$$i$$$ is forced to sign her (or his) name on $$$B_i$$$.
The blog defines $$$c(i)$$$ as:
$$$c(i) := max\{j|a_j < b_i\}$$$. Obviously $$$i \in \{j|a_j < b_i\}$$$ and $$$c(i) \geq i$$$.
In the Case $$$1$$$ where $$$c(i) = i$$$, no matter the person $$$i$$$ signs on $$$A_i$$$ and $$$B_i$$$, $$$i$$$ places in front of $$$i+1,\,i+2,\,...\,n$$$. Therefore, we have $$$f(i) = g(i) = f(i+1)$$$.
In the Case $$$2$$$, person $$$i$$$ does not interfere with $$$j+1,\,j+2,...\,n$$$. First, we consider $$$f(i)$$$. If $$$i$$$ signs on $$$A_i$$$, then no matter when $$$i+1,\,i+2,\,...\,n$$$ sign their name, $$$i$$$ is always placed in front of them. It is a bijection, given a permutation of $$$[i+1,\,i+2\,...\,n]$$$, just add $$$i$$$ in front of the permutation to get a new permutation of $$$[i,\,i+1\,...\,n]$$$. So, the number of distinct permutations when $$$i$$$ signs on $$$A_i$$$ is just $$$f(i+1)$$$.
Part3: What I have learned
(1) The most important idea in counting is dividing into non-overlapping sets whose union are the whole set.
(2) The most important thing of dp is defining states.
(3) Bijections are really important in combinatorics. For example, the Dyck path.
(4) Achieving (1) and (2) requires high IQ and talent. For example, the definition of $$$f$$$ in the blog is normal, while the definition of $$$g$$$ is genius!