#P9799. [NERC 2018] Interval-Free Permutations
[NERC 2018] Interval-Free Permutations
Background
Translated from Problem I of NERC 2018.
Problem Description
We define a permutation of to be an “interval permutation” if there exists a contiguous subarray of length such that, after sorting, the elements in this subarray form a sequence of consecutive natural numbers. For example, is an “interval permutation”, because , , and all become a consecutive sequence of natural numbers after sorting.
Given , output the number of permutations that are not “interval permutations”. Since the answer may be very large, output it modulo .
Input Format
The first line contains two integers and , representing the number of test cases and the modulus.
The next lines each contain one integer .
Output Format
For each test case, output the number of permutations of that are not “interval permutations”, modulo .
4 998244353
1
4
5
9
1
2
6
28146
1 437122297
20
67777575
Hint
The constraints guarantee , , and .
Explanation for Sample 1:
For the second test case, and satisfy the requirement.
For the third test case, , , , , , and satisfy the requirement.
For Sample 2, there are possibilities in total.
Translated by ChatGPT 5