Graph Coloring (hard version)
Contest: Educational Codeforces Round 142 (Rated for Div. 2)
Difficulty rating: 2900
Tags: brute force, combinatorics, divide and conquer, dp, fft, graphs

The only difference between the easy and the hard version is the constraint on $$$n$$$.

You are given an undirected complete graph on $$$n$$$ vertices. A complete graph is a graph where each pair of vertices is connected by an edge. You have to paint the edges of the graph into two colors, red and blue (each edge will have one color).

A set of vertices $$$S$$$ is red-connected if, for every pair of vertices $$$(v_1, v_2)$$$ such that $$$v_1 \in S$$$ and $$$v_2 \in S$$$, there exists a path from $$$v_1$$$ to $$$v_2$$$ that goes only through red edges and vertices from $$$S$$$. Similarly, a set of vertices $$$S$$$ is blue-connected if, for every pair of vertices $$$(v_1, v_2)$$$ such that $$$v_1 \in S$$$ and $$$v_2 \in S$$$, there exists a path from $$$v_1$$$ to $$$v_2$$$ that goes only through blue edges and vertices from $$$S$$$.

You have to paint the graph in such a way that:

- there is at least one red edge;
- there is at least one blue edge;
- for each set of vertices $$$S$$$ such that $$$|S| \ge 2$$$, $$$S$$$ is either red-connected or blue-connected, but not both.

Calculate the number of ways to paint the graph, and print it modulo $$$998244353$$$.

Input format:
The first (and only) line contains one integer $$$n$$$ ($$$3 \le n \le 5 \cdot 10^4$$$).

Output format:
Print one integer — the number of ways to paint the graph, taken modulo $$$998244353$$$.

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

Example input:
3

Example output:
6

Example input:
4

Example output:
50

Editorial:
Please read the tutorial for the easy version first, since this tutorial uses some definitions from it.
Okay, we need more definitions. Here they come:
$$$$$$C_0 = 0, C_i = \frac{A_i}{i!} \textrm{ if } i > 0$$$$$$
$$$$$$D_0 = 0, D_i = \frac{B_i}{(i-1)!} \textrm{ if } i > 0$$$$$$
This way, we can transform the formula for $$$B_n$$$ to the following:
$$$$$$B_n = (n-1)! \cdot \sum\limits_{k=1}^{n-1} C_{n-k} D_k$$$$$$.
Or even this, since $$$C_0 = D_0 = 0$$$:
$$$$$$B_n = (n-1)! \cdot \sum\limits_{k=0}^{n} C_{n-k} D_k$$$$$$.
This is almost the convolution of the sequences $$$C$$$ and $$$D$$$ (with a bit extra additional operations after the convolution), so, to compute the sequence $$$B$$$, we just need to compute the sequences $$$C$$$ and $$$D$$$, and then calculate their convolution with NTT. All that's left is to multiply every element by the corresponding factorial.
But wait, that's not so easy. In order to calculate $$$C_i$$$ and $$$D_i$$$, we need to know $$$B_i$$$. Note that we can ignore the fact that $$$C_i$$$ and $$$D_i$$$ appear in the formula for $$$B_i$$$, since they are multiplied by $$$0$$$, so at least we don't have a dependency cycle. Unfortunately, we cannot just straightforwardly use convolution if we don't know the sequences $$$C_i$$$ and $$$D_i$$$.
The model solution handles it using the following approach. Let's generate $$$A$$$, $$$B$$$, $$$C$$$ and $$$D$$$ in parallel: on the $$$i$$$-th iteration, calculate $$$B_i$$$, then calculate $$$A_i$$$, $$$C_i$$$ and $$$D_i$$$ using it. And sometimes we will calculate the convolution of the sequences $$$C$$$ and $$$D$$$.
Suppose we want to calculate $$$B_i$$$, and the last time we calculated the convolution of $$$C$$$ and $$$D$$$ was after the iteration $$$t$$$. Back then, we knew all elements from $$$C_0$$$ to $$$C_t$$$ and from $$$D_0$$$ to $$$D_t$$$. So, the $$$i$$$-th term in the convolution of $$$C$$$ and $$$D$$$ contained the sum of $$$C_{i-k} D_k$$$ over all $$$k$$$ such that $$$k \le t$$$ and $$$i - k \le t$$$. So, in order to calculate $$$B_i$$$, we have to pick this value from the convolution and then add the sum of $$$C_{i-k} D_k$$$ over all $$$k$$$ such that $$$k > t$$$ or $$$k \le i - t$$$, and there are $$$2(i-t)$$$ such values.
Suppose we compute the convolution every $$$K$$$ iterations. Then the maximum value of $$$i-t$$$ is $$$K$$$, and every value of $$$B_i$$$ is calculated in $$$O(K)$$$. We also make $$$\frac{n}{K}$$$ convolutions, so the total complexity of this solution will be $$$O(\frac{n^2 \log n}{K} + nK)$$$, which can be transformed into $$$O(n \sqrt{n \log n})$$$ if we pick $$$K = \sqrt{n \log n}$$$.