#P7444. 「EZEC-7」猜排列

    ID: 8268 远端评测题 1000ms 128MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP2021洛谷原创O2优化动态规划优化前缀和逆元洛谷月赛

「EZEC-7」猜排列

Background

Update: The testdata has been strengthened.

Problem Description

Alice has a permutation aa of length nn, and the numbers in the permutation are 0,1,2,,n10,1,2,\cdots,n-1.

Bob has nothing to do and wants to guess it, but Alice does not want him to guess it too easily.

So she gives Bob some conditions and asks him to guess the permutation.

We define f(l,r)=mex{al,al+1,,ar}f(l,r)=\text{mex}\{a_l,a_{l+1},\cdots,a_r\}, where the mex\text{mex} function means the smallest non-negative integer that does not appear in a multiset.

The conditions Alice gives contain nn numbers. The ii-th number represents the number of pairs (l,r)(l,r) satisfying 1lrn1 \leq l \leq r \leq n and f(l,r)=i1f(l,r)=i-1.

Bob immediately knows that this still cannot determine the whole permutation, so he wants to know how many permutations satisfy the given conditions.

Input Format

The first line contains a positive integer nn, representing the length of Alice's permutation.

The second line contains nn non-negative integers. The ii-th number cic_i represents the number of pairs (l,r)(l,r) satisfying lrl \leq r and f(l,r)=i1f(l,r)=i-1.

Output Format

Output the number of permutations satisfying the given conditions modulo 998244353998244353.

4
4 3 1 1
2
4
4 0 3 2
0

Hint

Sample Explanation

In the first sample, there are two permutations that satisfy the conditions:

{1,0,2,3}\{1,0,2,3\} and {3,2,0,1}\{3,2,0,1\}.

In the second sample, you can find by enumeration that there is no solution that satisfies the requirements.

Constraints

This problem uses bundled testdata.

  • Subtask 1 (4 points): n8n\leq 8.
  • Subtask 2 (8 points): n20n\leq 20.
  • Subtask 3 (16 points): n100n\leq 100.
  • Subtask 4 (32 points): n2×103n\leq 2\times 10^3.
  • Subtask 5 (20 points): n105n\leq 10^5.
  • Subtask 6 (20 points): no special constraints.

For 100%100\% of the testdata, 1n5×1051\le n\le5\times10^5, ci0c_i \ge 0, and it is guaranteed that i=1nci=n(n+1)21\sum^{n}_{i=1}c_i=\frac{n(n+1)}{2}-1.

Translated by ChatGPT 5