#arc162e. [arc162_e]Strange Constraints

[arc162_e]Strange Constraints

Problem Statement

You are given a sequence of length NN consisting of integers from 11 to NN, A=(A1,A2,ldots,AN)A=(A_1,A_2,\\ldots,A_N).

Find the number, modulo 998244353998244353, of sequences of length NN consisting of integers from 11 to NN, B=(B1,B2,ldots,BN)B=(B_1,B_2,\\ldots,B_N), that satisfy the following conditions for all i=1,2,ldots,Ni=1,2,\\ldots,N.

  • The number of occurrences of ii in BB is at most AiA_i.
  • The number of occurrences of BiB_i in BB is at most AiA_i.

Constraints

  • 1leqNleq5001 \\leq N \\leq 500
  • 1leqAileqN1 \\leq A_i \\leq N
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN A1A_1 A2A_2 ldots\\ldots ANA_N

Output

Print the answer.


Sample Input 1

3
1 2 3

Sample Output 1

10

The following 1010 sequences satisfy the conditions:

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

Sample Input 2

4
4 4 4 4

Sample Output 2

256

All sequences of length 44 consisting of integers from 11 to 44 satisfy the conditions, and there are 44=2564^4=256 such sequences.


Sample Input 3

5
1 1 1 1 1

Sample Output 3

120

All permutations of (1,2,3,4,5)(1,2,3,4,5) satisfy the conditions, and there are 5!=1205!=120 such sequences.


Sample Input 4

14
6 5 14 3 6 7 3 11 11 2 3 7 8 10

Sample Output 4

628377683

Be sure to print the number modulo 998244353998244353.