周游世界AI (CI翻译)
There are $n$ islands, numbered from $1$ to $n$. The $i$-th island has value $a_i$. The values $a_1,a_2,\ldots,a_n$ are strictly increasing, and $a_1=0$.
有 $n$ 座岛屿,编号从 $1$ 到 $n$。第 $i$ 座岛屿的值为 $a_i$。数值 $a_1,a_2,\ldots,a_n$ 严格递增,且 $a_1=0$。AI (CI翻译)
Farmer John wants to rearrange the islands. After rearranging them, their values form an array $b_1,b_2,\ldots,b_n$, which is a rearrangement of $a$.
Farmer John 想要重新排列这些岛屿。重新排列后,它们的值构成数组 $b_1,b_2,\ldots,b_n$,且 $b$ 是 $a$ 的一个排列。AI (CI翻译)
Bessie then travels between islands according to the following rule. Suppose she is currently on the island in position $i$ after the rearrangement. Then Bessie may travel from the island in position $i$ to the island in position $j$ if $$ b_i + b_j = \max(b_i,b_{i+1},\ldots,b_n). $$
随后,Bessie 按照以下规则在岛屿之间旅行。假设她当前位于重新排列后位置 $i$ 的岛屿,那么当 $$ b_i + b_j = \max(b_i,b_{i+1},\ldots,b_n). $$ 时,她可以从位置 $i$ 的岛屿前往位置 $j$ 的岛屿。AI (CI翻译)
A rearrangement $b$ is called good if there exists a sequence of distinct positions $p_1,p_2,\ldots,p_n$ such that Bessie can travel from $p_i$ to $p_{i+1}$ for every $1\le i \lt n$. In other words, Bessie can visit all positions (and therefore all islands) exactly once by following valid travels.
如果存在一个由互不相同的位置组成的序列 $p_1,p_2,\ldots,p_n$,使得对于每个 $1\le i \lt n$,Bessie 都可以从 $p_i$ 前往 $p_{i+1}$,则称排列 $b$ 是好的。换句话说,Bessie 可以通过合法旅行恰好访问每个位置(因而也访问每座岛屿)一次。AI (CI翻译)
Count the number of good rearrangements of the islands. Since the number can be large, output it modulo $10^9+7$.
请统计岛屿的好排列数量。由于答案可能很大,请对 $10^9+7$ 取模后输出。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个输入包含多组测试用例。第一行包含测试用例数量 $t$($1 \le t \le 10^4$)。接下来是各测试用例的描述。AI (CI翻译)
The first line of each test case contains $n$ ($4 \le n \le 2 \cdot 10^5$).
每个测试用例的第一行包含一个整数 $n$($4 \le n \le 2 \cdot 10^5$)。AI (CI翻译)
The second line of each test case contains $n$ distinct integers $a_1,a_2,\ldots,a_n$ ($0\le a_i\le 10^9$).
第二行包含 $n$ 个互不相同的整数 $a_1,a_2,\ldots,a_n$($0\le a_i\le 10^9$)。AI (CI翻译)
It is guaranteed that $a_1=0$ and $a_1 \lt a_2 \lt \cdots \lt a_n$.
保证 $a_1=0$ 且 $a_1 \lt a_2 \lt \cdots \lt a_n$。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
保证所有测试用例的 $n$ 之和不超过 $2\cdot 10^5$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output a single integer — the number of good rearrangements $b$ of $a$, modulo $10^9+7$.
对于每个测试用例,输出一个整数,表示 $a$ 的好排列 $b$ 的数量,对 $10^9+7$ 取模。AI (CI翻译)
Note
说明
AI (CI翻译)For the first testcase, one $b$ that works is $[2,1,0,5,3,4]$.
对于第一个测试用例,一种可行的排列 $b$ 是 $[2,1,0,5,3,4]$。AI (CI翻译)
Note that Bessie can travel from $1 \to 5$ since $2 + 3 = b_1 + b_5 = \max(b_1,b_2,\ldots,b_6) = 5$.
注意,Bessie 可以从 $1 \to 5$,因为 $2 + 3 = b_1 + b_5 = \max(b_1,b_2,\ldots,b_6) = 5$。AI (CI翻译)
Similarly, Bessie can travel from $5 \to 2$, $2\to 6$, $6\to 3$, $3\to 4$.
类似地,Bessie 可以从 $5 \to 2$、$2\to 6$、$6\to 3$、$3\to 4$。AI (CI翻译)
Thus, starting from island $1$, Bessie can follow the path $1\to 5\to 2\to 6\to 3\to 4$ which visits every island. Therefore, this rearrangement is good.
因此,从岛屿 $1$ 出发,Bessie 可以沿着路径 $1\to 5\to 2\to 6\to 3\to 4$ 行进,从而访问每座岛屿。所以这个排列是好的。AI (CI翻译)
For the second testcase, it can be shown that there are no good rearrangements. Therefore, the answer is $0$.
对于第二个测试用例,可以证明不存在好的排列。因此答案为 $0$。AI (CI翻译)
样例
3
6
0 1 2 3 4 5
5
0 3 4 5 6
4
0 1 3 4
12
0
4
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。