CF1699C.The Third Problem

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given a permutation a1,a2,…,ana_1,a_2,\ldots,a_n of integers from 00 to n−1n - 1. Your task is to find how many permutations b1,b2,…,bnb_1,b_2,\ldots,b_n are similar to permutation aa.

Two permutations aa and bb of size nn are considered similar if for all intervals [l,r][l,r] (1≤l≤r≤n1 \le l \le r \le n), the following condition is satisfied: $$\operatorname{MEX}([a_l,a_{l+1},\ldots,a_r])=\operatorname{MEX}([b_l,b_{l+1},\ldots,b_r]),$$ where the MEX⁡\operatorname{MEX} of a collection of integers c1,c2,…,ckc_1,c_2,\ldots,c_k is defined as the smallest non-negative integer xx which does not occur in collection cc. For example, MEX⁡([1,2,3,4,5])=0\operatorname{MEX}([1,2,3,4,5])=0, and MEX⁡([0,1,2,4,5])=3\operatorname{MEX}([0,1,2,4,5])=3.

Since the total number of such permutations can be very large, you will have to print its remainder modulo 109+710^9+7.

In this problem, a permutation of size nn is an array consisting of nn distinct integers from 00 to n−1n-1 in arbitrary order. For example, [1,0,2,4,3][1,0,2,4,3] is a permutation, while [0,1,1][0,1,1] is not, since 11 appears twice in the array. [0,1,3][0,1,3] is also not a permutation, since n=3n=3 and there is a 33 in the array.

给你一个由 00 到 n−1n-1 的整数构成的排列 a1,a2,…,ana_1,a_2,\ldots,a_n。你的任务是求出有多少个排列 b1,b2,…,bnb_1,b_2,\ldots,b_n 与排列 aa 相似。

对于大小为 nn 的两个排列 aa 和 bb,若对所有区间 [l,r][l,r](其中 1≤l≤r≤n1 \le l \le r \le n),均满足以下条件,则称它们相似:

MEX⁡([al,al+1,…,ar])=MEX⁡([bl,bl+1,…,br]),\operatorname{MEX}([a_l,a_{l+1},\ldots,a_r])=\operatorname{MEX}([b_l,b_{l+1},\ldots,b_r]),

其中,集合 c1,c2,…,ckc_1,c_2,\ldots,c_k 的 MEX⁡\operatorname{MEX} 定义为未出现在该集合中的最小非负整数 xx。例如,MEX⁡([1,2,3,4,5])=0\operatorname{MEX}([1,2,3,4,5])=0,而 MEX⁡([0,1,2,4,5])=3\operatorname{MEX}([0,1,2,4,5])=3。

由于满足条件的排列总数可能非常大,你只需输出其对 109+710^9+7 取模的结果。

在本题中,大小为 nn 的排列是指由 00 到 n−1n-1 的 nn 个互不相同的整数组成的任意顺序的数组。例如,[1,0,2,4,3][1,0,2,4,3] 是一个排列,而 [0,1,1][0,1,1] 不是,因为 11 在数组中出现了两次;[0,1,3][0,1,3] 也不是排列,因为此时 n=3n=3,但数组中包含了 33(超出了 00 到 n−1n-1 的范围)。

输入格式

Each test contains multiple test cases. The first line of input contains one integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases. The following lines contain the descriptions of the test cases.

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the size of permutation aa.

The second line of each test case contains nn distinct integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai<n0 \le a_i \lt n) — the elements of permutation aa.

It is guaranteed that the sum of nn across all test cases does not exceed 10510^5.

每个测试包含多个测试用例。输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4),表示测试用例的数量。接下来的行包含各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5),表示排列 aa 的长度。

每个测试用例的第二行包含 nn 个互不相同的整数 a1,a2,…,ana_1,a_2,\ldots,a_n(0≤ai<n0 \le a_i \lt n),即排列 aa 的元素。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case, print a single integer, the number of permutations similar to permutation aa, taken modulo 109+710^9+7.

对于每个测试用例,输出一个整数,表示与排列 aa 相似的排列的数量,结果对 109+710^9+7 取模。

输入输出样例

  • 输入#1

    5
    5
    4 0 3 2 1
    1
    0
    4
    0 1 2 3
    6
    1 2 4 0 5 3
    8
    1 3 7 2 5 0 6 4

    输出#1

    2
    1
    1
    4
    72

说明/提示

For the first test case, the only permutations similar to a=[4,0,3,2,1]a=[4,0,3,2,1] are [4,0,3,2,1][4,0,3,2,1] and [4,0,2,3,1][4,0,2,3,1].

For the second and third test cases, the given permutations are only similar to themselves.

For the fourth test case, there are 44 permutations similar to a=[1,2,4,0,5,3]a=[1,2,4,0,5,3]:

  • [1,2,4,0,5,3][1,2,4,0,5,3];
  • [1,2,5,0,4,3][1,2,5,0,4,3];
  • [1,4,2,0,5,3][1,4,2,0,5,3];
  • [1,5,2,0,4,3][1,5,2,0,4,3].

对于第一个测试用例,唯一与 a=[4,0,3,2,1]a=[4,0,3,2,1] 相似的排列是 [4,0,3,2,1][4,0,3,2,1] 和 [4,0,2,3,1][4,0,2,3,1]。

对于第二个和第三个测试用例,给定的排列仅与自身相似。

对于第四个测试用例,共有 44 个排列与 a=[1,2,4,0,5,3]a=[1,2,4,0,5,3] 相似:

  • [1,2,4,0,5,3][1,2,4,0,5,3];
  • [1,2,5,0,4,3][1,2,5,0,4,3];
  • [1,4,2,0,5,3][1,4,2,0,5,3];
  • [1,5,2,0,4,3][1,5,2,0,4,3]。

输入解题思路,AI测评打分。不知道怎么写?

首页