THIRD プログラミングコンテスト 2023 アルゴ(AtCoder Beginner Contest 318)
Ex
Count Strong Test Cases
AtCoder
⌀ 2785
原题
←G →
翻译中…

Problem Statement

翻译中…

Snuke has come up with the following problem.

翻译中…

You are given permutations $P=(P_1,P_2,\ldots,P_N)$ and $Q=(Q_1,Q_2,\ldots,Q_N)$ of $(1,2,\ldots,N)$. Let us build a graph with $N$ vertices and $N$ edges as follows.

  • For $i=1,2,\ldots,N$ in this order, draw an edge of weight $Q_i$ connecting vertices $i$ and $P_i$ bidirectionally.

When removing some number of edges to eliminate cycles from the graph, find the minimum possible total weight of the removed edges.

翻译中…

Alice and Bob came up with the following solutions.

翻译中…

Alice: Initialize the answer to $0$. For $i=1,2,\ldots,N$ in this order, if the edge connecting vertices $i$ and $P_i$ is contained in a cycle, remove that edge and add its weight to the answer.

翻译中…

Bob: Initialize the answer to $0$. For $i=N,N-1,\ldots,1$ in this order, if the edge connecting vertices $i$ and $P_i$ is contained in a cycle, remove that edge and add its weight to the answer.

翻译中…

Snuke has realized that their solutions are both incorrect, and he wants to know the number of inputs for which neither of their solutions gives the correct answer.

翻译中…

Among the $(N!)^2$ possible inputs, find the number, modulo $998244353$, of inputs for which neither Alice's nor Bob's solution gives the correct answer.

翻译中…

Constraints

翻译中…
  • $1\leq N\leq 2\times 10^5$
  • All input values are integers.
翻译中…

Input

翻译中…

The input is given from Standard Input in the following format:

翻译中…

N

翻译中…

Output

翻译中…

Print the answer as an integer.

翻译中…

样例

Input #1
3
Output #1
4
The following four inputs satisfy the condition. - $P=(2,3,1),Q=(2,1,3)$ - $P=(2,3,1),Q=(3,1,2)$ - $P=(3,1,2),Q=(2,1,3)$ - $P=(3,1,2),Q=(3,1,2)$ For example, for the input $P=(2,3,1),Q=(2,1,3)$, the correct answer is $1$, but Alice's solution gives $2$ and Bob's gives $3$.
Input #2
2
Output #2
0
There may be no inputs that satisfy the condition.
Input #3
6
Output #3
314708
Input #4
318
Output #4
321484323
题目信息

题面与样例版权归 AtCoder 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。