AtCoder Regular Contest-- 219
B
反转排列
Reverse Permutation
AtCoder
⌀ 891
原题
← AC →

反转排列AI (CI翻译)

Problem Statement

题目描述

AI (CI翻译)

You are given an integer $N$ and a permutation $P=(P_1,P_2,\ldots,P_N)$ of $(1,2,\ldots,N)$.

给定一个整数 $N$ 和一个 $(1,2,\ldots,N)$ 的排列 $P=(P_1,P_2,\ldots,P_N)$。AI (CI翻译)

For a permutation $Q=(Q_1,Q_2,\ldots,Q_N)$ of $(1,2,\ldots,N)$, let $Q'=(Q_1',Q_2',\ldots,Q_N')$ be the lexicographically smallest permutation obtainable by performing the following operation exactly once:

对于一个 $(1,2,\ldots,N)$ 的排列 $Q=(Q_1,Q_2,\ldots,Q_N)$,令 $Q'=(Q_1',Q_2',\ldots,Q_N')$ 表示恰好进行一次以下操作后能够得到的字典序最小的排列:AI (CI翻译)

  • Choose a pair of integers $(l,r)$ satisfying $1\le l\le r\le N$, and reverse $Q_l,Q_{l+1},\ldots,Q_r$. More precisely, replace $Q$ with $(Q_1,Q_2,\ldots,Q_{l-1},Q_r,Q_{r-1},\ldots,Q_l,Q_{r+1},Q_{r+2},\ldots,Q_N)$.
  • 选择一对满足 $1\le l\le r\le N$ 的整数 $(l,r)$,将 $Q_l,Q_{l+1},\ldots,Q_r$ 反转。更具体地说,将 $Q$ 替换为 $(Q_1,Q_2,\ldots,Q_{l-1},Q_r,Q_{r-1},\ldots,Q_l,Q_{r+1},Q_{r+2},\ldots,Q_N)$。
AI (CI翻译)

Find the number, modulo $998244353$, of permutations $Q$ of $(1,2,\ldots,N)$ such that $Q'=P$.

求满足 $Q'=P$ 的 $(1,2,\ldots,N)$ 的排列 $Q$ 的数量。答案对 $998244353$ 取模。AI (CI翻译)

You are given $T$ test cases; solve each of them.

给定 $T$ 组测试数据,请分别求出每组测试数据的答案。AI (CI翻译)

Constraints

约束条件

AI (CI翻译)
  • $1\le T$
  • $1\le N\le 5\times 10^5$
  • $P$ is a permutation of $(1,2,\ldots,N)$.
  • The sum of $N$ over all test cases is at most $5\times 10^5$.
  • All input values are integers.
  • $1\le T$
  • $1\le N\le 5\times 10^5$
  • $P$ 是一个 $(1,2,\ldots,N)$ 的排列。
  • 所有测试数据的 $N$ 之和不超过 $5\times 10^5$。
  • 所有输入值均为整数。
AI (CI翻译)

Input

输入格式

AI (CI翻译)

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

输入从标准输入中按以下格式给出:AI (CI翻译)

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T

T
\text{case}_1
\text{case}_2
\vdots
\text{case}_T
AI (CI翻译)

Each test case is given in the following format:

每组测试数据按以下格式给出:AI (CI翻译)

N
P_1 P_2 \ldots P_N

N
P_1 P_2 \ldots P_N
AI (CI翻译)

Output

输出格式

AI (CI翻译)

Output the answers for the test cases in order, separated by newlines.

按顺序输出各组测试数据的答案,每个答案占一行。AI (CI翻译)

样例

Input #1
4
3
1 3 2
1
1
4
4 3 2 1
6
1 2 6 4 5 3
Output #1
2
1
0
9
Consider the first test case. For example, when $Q=(2,3,1)$, choosing $(l,r)=(1,3)$ gives $(1,3,2)$. No permutation lexicographically smaller than $(1,3,2)$ can be obtained, so we have $Q'=(1,3,2)$. Thus, $Q=(2,3,1)$ satisfies $Q'=P$. There are two permutations $Q$ such that $Q'=P$: $Q=(2,3,1),(3,1,2)$.
题目信息
题号
arc219_b
时间限制
2 s
内存限制
1024 MB

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