Typical Permutation Descriptor

AtCoder
IDarc186_b
Time2000ms
Memory256MB
Difficulty
You are given a sequence of integers $(A_1,\dots,A_N)$ of length $N$. This sequence satisfies $0\le A_i < i$ for each $i=1,\dots,N$. Find the number of permutations $(P_1,\dots,P_N)$ of $(1,\dots,N)$ that satisfy the following conditions, modulo $998244353$. * For each $i=1,\dots,N$: * $P_j > P_i$ for any integer $j$ with $A_i < j < i$ * $P_{A_i} < P_i$ if $A_i > 0$ For the sequence $(A_1,\dots,A_N)$ given in the input, it is guaranteed that there exists a permutation satisfying the conditions. ## Constraints * $1\le N\le 3\times 10^5$ * $0\le A_i \lt i$ * For $A_1,\dots,A_N$, there exists a permutation satisfying the conditions in the problem statement. * All input values are integers. ## Input The input is given from Standard Input in the following format: $N$ $A_1$ $A_2$ $\dots$ $A_N$ [samples]
Samples
Input #1
4
0 1 0 3
Output #1
3

There are three such permutations: $(2, 3, 1, 4)$, $(2, 4, 1, 3)$, and $(3, 4, 1, 2)$.
Input #2
22
0 1 2 2 2 2 2 2 1 9 9 9 9 0 14 15 15 15 14 19 19 19
Output #2
353820794

The answer is $353820794$, which is $2350309500$ modulo $998244353$.
API Response (JSON)
{
  "problem": {
    "name": "Typical Permutation Descriptor",
    "description": {
      "content": "You are given a sequence of integers $(A_1,\\dots,A_N)$ of length $N$. This sequence satisfies $0\\le A_i < i$ for each $i=1,\\dots,N$. Find the number of permutations $(P_1,\\dots,P_N)$ of $(1,\\dots,N)$ ",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "arc186_b"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given a sequence of integers $(A_1,\\dots,A_N)$ of length $N$. This sequence satisfies $0\\le A_i < i$ for each $i=1,\\dots,N$. Find the number of permutations $(P_1,\\dots,P_N)$ of $(1,\\dots,N)$ ...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments