题目收集
题目列表题单
返回列表

Patient Stack

CodeforcesP0062· 1000ms· 256 MB· ZJU Summer 2026 Contest 6原题链接
dp

创建于 2026/07/18 · 更新于 2026/07/18

题目描述

You start with an empty stack and push the elements 1,2,…,n1, 2, \ldots, n1,2,…,n in this exact increasing order, each exactly once. Between the pushes, you may interleave any number of pop operations, each removing the current top of the stack.

A sequence of operations is complete if, in the end, every element has been pushed and the stack is empty (so it contains exactly nnn pushes and nnn pops, and at no moment have there been more pops than pushes).

Each element iii has a patience aia_iai​: after element iii is pushed, at least aia_iai​ more push operations must occur before iii is allowed to be popped. Since the elements are pushed in increasing order, this is the same as saying that element iii may be popped only after element i+aii + a_ii+ai​ has been pushed (that is, once all of i+1,i+2,…,i+aii+1, i+2, \ldots, i+a_ii+1,i+2,…,i+ai​ are in). In particular, if i+ai>ni + a_i \gt ni+ai​>n, there are not enough later elements, so element iii can never be popped.

Count the complete operation sequences that satisfy the patience constraint of every element, modulo 998244353998244353998244353.

输入格式

The first line contains a single integer nnn.

The second line contains nnn integers a1,a2,…,ana_1, a_2, \ldots, a_na1​,a2​,…,an​.

1≤n≤50001 \le n \le 50001≤n≤5000, 0≤ai≤n0 \le a_i \le n0≤ai​≤n.

输出格式

Print a single integer — the number of valid complete operation sequences, modulo 998244353998244353998244353.

Note that if some element iii satisfies ai>n−ia_i \gt n - iai​>n−i, then fewer than aia_iai​ pushes remain after it, so it can never be popped in time, and the answer is 000.

样例输入 #1

3
0 0 0

样例输出 #1

5

样例输入 #2

4
2 0 0 0

样例输出 #2

7

样例输入 #3

2
2 0

样例输出 #3

0