#ARC146E. [ARC146E] Simple Speed

[ARC146E] Simple Speed

Score : 800800 points

Problem Statement

You are given a sequence of NN positive integers: A=(A1,A2,,AN)A=(A_1,A_2,\dots,A_N).

How many integer sequences BB consisting of integers between 11 and NN (inclusive) satisfy all of the following conditions? Print the count modulo 998244353998244353.

  • For each integer ii such that 1iN1 \le i \le N, there are exactly AiA_i occurrences of ii in BB.
  • For each integer ii such that 1iB11 \le i \le |B|-1, it holds that BiBi+1=1|B_i - B_{i+1}|=1.

Constraints

  • 1N2×1051 \le N \le 2 \times 10^5
  • 1Ai2×1051 \le A_i \le 2 \times 10^5
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN

A1A_1 A2A_2 \dots ANA_N

Output

Print the answer.

3
2 3 1
6

BB can be the following six sequences.

  • (1,2,1,2,3,2)(1,2,1,2,3,2)
  • (1,2,3,2,1,2)(1,2,3,2,1,2)
  • (2,1,2,1,2,3)(2,1,2,1,2,3)
  • (2,1,2,3,2,1)(2,1,2,3,2,1)
  • (2,3,2,1,2,1)(2,3,2,1,2,1)
  • (3,2,1,2,1,2)(3,2,1,2,1,2)

Thus, the answer is 66.

1
200000
0

There may be no sequence that satisfies the conditions.

6
12100 31602 41387 41498 31863 12250
750337372