Problem:
 Score : 300 points    Problem Statement Given is an integer sequence D_1,...,D_N of N elements. Find the number, modulo 998244353 , of trees with N vertices numbered 1 to N that satisfy the following condition:   For every integer i from 1 to N , the distance between Vertex 1 and Vertex i is D_i .       Notes  A tree of N vertices is a connected undirected graph with N vertices and N-1 edges, and the distance between two vertices are the number of edges in the shortest path between them.  Two trees are considered different if and only if there are two vertices x and y such that there is an edge between x and y in one of those trees and not in the other.       Constraints  1 \leq N \leq 10^5  0 \leq D_i \leq N-1         Input Input is given from Standard Input in the following format:  N  D_1  D_2  ...  D_N       Output Print the answer.        Sample Input 1 4
0 1 1 2      Sample Output 1 2  For example, a tree with edges (1,2) , (1,3) , and (2,4) satisfies the condition.       Sample Input 2 4
1 1 1 1      Sample Output 2 0       Sample Input 3 7
0 3 2 1 2 2 1      Sample Output 3 24

Solution (Python):
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
if A[0] != 0:
  print(0)
  exit()
AC = list(Counter(A).items())
AC.sort()
MOD = 998244353
ans = 1
last_v = 1
last_k = -1
for k, v in AC:
  if k == 0:
    if v != 1:
      print(0)
      exit()
    last_k = 0
    continue
  if k != last_k+1:
    print(0)
    exit()
  ans = ans*pow(last_v, v, MOD)%MOD
  last_v = v
  last_k = k
print(ans)

Solution (Python):
import re
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def v(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 6)
mod = 10**9+7
cnt = 0
ans = 0
inf = float("inf")
al = "abcdefghijklmnopqrstuvwxyz"
AL = al.upper()

n = k()
d = l()

count = Counter(d)
if d[0] == 0 and d.count(0) == 1:
    ans = 1
else:
    ans = 0

for i in range(max(count)):
    ans *= count[i]**count[i+1]
    ans %= 998244353
print(ans)

Solution (Python):
N=int(input())
D=list(map(int,input().split()))

isOK=True
if D[0]!=0:
    isOK=False

for i in range(1,N):
    if D[i]==0:
        isOK=False
        break

cnt=[0]*(max(D)+1)
MOD=998244353
for i in range(N):
    cnt[D[i]]+=1

ans=1
for i in range(len(cnt)-1):
    
    for r in range(cnt[i+1]):
        ans=ans*cnt[i]%MOD
if isOK:
    print(ans)
else:
    print(0)

Solution (Python):
import collections
n=int(input())
a=list(map(int,input().split()))
if a[0]==0 and a.count(0)==1:
  ans=1
else:
  ans=0
a=collections.Counter(a)
for i in range(max(a)):
  ans*=a[i]**a[i+1]
  ans%=998244353
print(ans)

Solution (Python):
n = int(input())
d = list(map(int, input().split()))
dist = [0]*n

for i in range(n):
    dist[d[i]] += 1

ans = 1

if d[0]!=0:
    ans = 0
elif dist[0] != 1:
    ans = 0
else:
    for i in range(2,n):
        if dist[i-1]==0 and dist[i]!=0:
            ans = 0
            break
        elif dist[i] == 0:
            continue
        else:
            for j in range(dist[i]):
                ans *= dist[i-1]
            if ans > 998244353:
                ans %= 998244353

print(ans)

Solution (Python):
n = int(input())
d = list(map(int ,input().split()))
if d[0] != 0:
  print (0)
  exit()
d.sort()
mod = 998244353
s = [0 for i in range(max(d)+1)]

for i in range(n):
  s[d[i]] += 1
ans = 1
for i in range(len(s)):
  if s[i] == 0:
    print (0)
    exit()
  if i == 0:
    if s[i] > 1:
      print (0)
      exit()
    continue
  ans *= pow(s[i-1],(s[i]),mod)
  ans %= mod
print (ans%mod)

Solution (Python):
N,D=open(0)
*D,=map(int,D.split())
c=[0]*-~max(D)
for i in D:c[i]+=1
a=D[0]<1==c[0]
for i,j in zip(c,c[1:]):a*=i**j
print(a%998244353)

Solution (Python):
N,*D=map(int,open(0).read().split())
c=[0]*N
for i in D:c[i]+=1
m=max(D)
if D[0]>0 or c[0]>1:print(0);exit()
a=1
for i in range(m):a*=c[i]**c[i+1]
print(a%998244353)

Solution (Python):
n=int(input())
mod=998244353
A=list(map(int,input().split()))
import sys

from math import sqrt, ceil
from collections import deque, Counter, defaultdict 
input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  
  
from functools import lru_cache
from bisect import bisect_left as bileft, bisect_right as biright
from fractions import Fraction as frac  






C=Counter(A)
if A[0]!=0 or A.count(0)>1:
    print(0);exit()
Q=[C[i] for i in range(max(A)+1)]

ans=1
for i  in range(len(Q)-1):
    ans*= pow(Q[i],Q[i+1],mod)
    ans%=mod

print(ans)

Solution (Python):
from sys import exit
from collections import Counter
mod = 998244353
n = int(input())
D = list(map(int, input().split()))
m = max(D)
C = Counter(D)
ans = 1
if C.get(0):
    if C[0] != 1 or D[0] != 0:
        print(0)
        exit()
else:
    print(0)
    exit()
for i in range(0, m):
    if C.get(i + 1):
        ans *= (C[i] ** C[i + 1]) % mod
        ans %= mod
    else:
        print(0)
        exit()

print(ans)

Solution (Python):
import bisect,collections,copy,itertools,math,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def main():




    n = I()
    d = LI()
    mod = 998244353
    
    dcnt = [0]*(max(d)+1)
    

    for x in d:
        dcnt[x] += 1

    nexist = d[0]!=0 or 0 in dcnt or 0 in d[1:]
    if nexist:
        print(0)
        exit(0)

    
    ans = 1

    for i in range(1, len(dcnt)):
        ans *= pow(dcnt[i-1], dcnt[i], mod)
        ans %= mod

    print(ans)
                    


main()

Solution (Python):
from collections import Counter
 
 
def solve(n, ddd):
    if ddd[0] != 0:
        return 0
    cnt = Counter(ddd)
    if cnt[0] != 1:
        return 0
    max_c = max(cnt)
    for i in range(max_c + 1):
        if i not in cnt:
            return 0
 
    MOD = 998244353
    ans = 1
    for i in range(max_c):
        prev = cnt[i]
        curr = cnt[i + 1]
        ans = ans * pow(prev, curr, MOD) % MOD
    return ans
 
 
n = int(input())
ddd = list(map(int, input().split()))
 
print(solve(n, ddd))

Solution (Python):
from collections import Counter

MOD = 998244353
N = int(input())
D = list(map(int, input().split()))

if D[0] != 0:
    print(0)
    exit()

C = sorted(Counter(D).most_common(), key=lambda x: x[0])

if C[0][1] > 1:
    print(0)
    exit()

ans = 1
for i in range(1, len(C)):
    if C[i][0] - C[i-1][0] > 1:
        print(0)
        exit()
    ans *= C[i - 1][1] ** C[i][1]

print(ans % MOD)

Solution (Python):

def resolve():
    MOD = 998244353
    N = int(input())
    D = list(map(int, input().split()))

    if D[0] != 0:
        print(0)
        return

    CNT = [0] * N
    for i in D:
        CNT[i] += 1

    if CNT[0] > 1:
        print(0)
        return

    ans = 1
    for i in range(1, N):
        ans *= pow(CNT[i - 1], CNT[i], MOD)
        ans %= MOD

    print(ans)


if __name__ == "__main__":
    resolve()

Solution (Python):

from collections import Counter
def resolve():
    MOD = 998244353
    N = int(input())
    D = list(map(int, input().split()))

    if D[0] != 0:
        print(0)
        return

    CNT = Counter(D)
    if CNT[0] > 1:  
        print(0)
        return

    ans = 1
    for i in range(1, max(D) + 1):
        ans *= pow(CNT[i - 1], CNT[i], MOD)
        ans %= MOD

    print(ans)


if __name__ == "__main__":
    resolve()

Solution (Python):
from collections import defaultdict
n = int(input())
lis = list(map(int, input().split()))

mod = 998244353

if lis[0] != 0:
    print(0)
    exit()

d = defaultdict(int)

for i in lis:
    d[i] += 1

if d[0] != 1:
    print(0)
    exit()

ans = 1
for i in range(1, max(lis)+1):
    if i not in d:
        print(0)
        exit()
    ans = ans * (d[i-1]**d[i]) % mod

print(ans)

Solution (Python):
import sys
from sys import exit
from collections import deque
from bisect import bisect_left, bisect_right, insort_left, insort_right 
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def perm(n,mod=None):
    ans = 1
    for i in range(1,n+1):
        ans *= i
        if mod!=None:
            ans %= mod
    return ans
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    return C

MOD=998244353
N,D = ilint()
if D[0]!=0:
    print(0)
    exit()
C = [0]*N
C[0]=1
for d in D[1:]:
    if d==0:
        print(0)
        exit()
    C[d] += 1
ans=1
for i in range(1,max(D)+1):
    ans *= powmod(C[i-1],C[i],MOD)
    ans %= MOD
print(ans)

Solution (Python):

import sys
from collections import Counter
n=int(input())
d=list(map(int,input().split()))
e=Counter(d)
mod=998244353
if d[0]!=0 or e[0]!=1:
    print(0)
    sys.exit()
ans=1
for i in range(2,n):
    ans*=pow(e[i-1],e[i],mod)
    ans%=mod
print(ans%mod)

Solution (Python):
N = int(input())
D = list(map(int, input().split()))

if D[0] != 0:
    print(0)
else:
    tmp = [0 for _ in range(10 ** 5)]
    for i in range(N):
        tmp[D[i]] += 1

    if tmp[0] != 1:
        print(0)
    else:
        ans = 1
        flag = False
        flag2 = False
        for i in range(1, 10 ** 5 - 1):

            if flag2 and tmp[i+1] != 0:
                ans = 0
                break

            if tmp[i+1] == 0:
                flag2 = True

            ans = (ans * (tmp[i] ** tmp[i+1])) % 998244353

        print(ans)

Solution (Python):
from collections import Counter
N=int(input())
D=list(map(int,input().split()))
mod=998244353
if D[0] != 0:
    print(0)
else:
    ans=1
    del D[0]
    if 0 in D:
        print(0)
    else:
        numbers=Counter(D)
        for n in range(2,max(numbers.keys())+1):
            if numbers[n]==None:
                ans=0
                break
            ans*=numbers[n-1]**numbers[n]
        print(ans%mod)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
r=1 if d[0]==0 else 0
d=d[1:]
d.sort()
c=1
nc=0
j=1
mod=998244353
for i in d:
  if i<j:
    r=0
  elif i==j:
    nc+=1
    r=(r*c)%mod
  elif i==j+1:
    j=i
    c=nc
    nc=1
    r=(r*c)%mod
  else:
    r=0
print(r)

Solution (Python):
from collections import Counter

N=int(input())
*d,=map(int,input().split())
mod = 998244353

if d[0]:
  print(0)
  exit()
  
dmax = max(d)
if sorted(set(d)) != list(range(dmax+1)):
  print(0)
  exit()

c = Counter(d)

if c[0]>1:
  print(0)
  exit()

ans =1
for i in range(2,dmax+1):
  ans *= pow(c[i-1],c[i],mod)
  ans %= mod
  
print(ans)

Solution (Python):
import collections
n = int(input())
d = list(map(int, input().split()))
mod = 998244353
f = collections.Counter(d)

if d[0] != 0 or f[0] != 1:  
    print(0)
    exit()
ans = 1
box = 1  
maxd = max(d)

for i in range(maxd+1):
    ans = ans*box**f[i]
    ans %= mod
    box = f[i]
print(ans)

Solution (Python):
from collections import Counter
mod = 998244353
n = int(input())
d = list(map(int,input().split()))
if d[0] != 0:
    print(0)
    exit()

C = Counter(d)
C = dict(sorted(C.items()))

memo1 = C[1]
memo2 = 1
ans = 1

for i in range(1,n):
    if d[i] == 0:
        print(0)
        exit()

for key in C:
    if key >= 2:
        if memo2 + 1 != key: 
            print(0)
            exit()
        ans = (ans * memo1 ** C[key]) % mod 
        memo1 = C[key]
        memo2 = key
print(ans)

Solution (Python):
from collections import Counter
n = int(input())
D = list(map(int,input().split()))
if D[0] != 0:
    print(0)
    exit()

C = Counter(D)
C = dict(sorted(C.items()))

memo1 = C[1]
memo2 = 1
ans = 1
for i in range(1,n):
    if D[i] == 0:
        print(0)
        exit()

for key in C:
    if key >= 2:
        if memo2 + 1 != key:
            print(0)
            exit()
        ans = (ans * (pow(memo1,C[key])))%998244353
        memo1 = C[key]
        memo2 = key
print(ans)

Solution (Python):
import sys
MOD = 998244353


def main():
    n, *d = map(int, sys.stdin.buffer.read().split())
    e = [0]*n
    for i in d:
        e[i] += 1
    ans = e[0]
    for i in d[1:]:
        ans = ans*e[i-1] % MOD
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
MOD = 998244353


def main():
    n, *d = map(int, sys.stdin.buffer.read().split())
    e = [0]*n
    for i in d:
        e[i] += 1
    ans = e[0]
    for i in d[1:]:
        ans *= e[i-1]
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
import sys
MOD = 998244353


def main():
    n, *d = map(int, sys.stdin.buffer.read().split())
    e = [0]*n
    for i in d:
        e[i] += 1
    ans = e[0]
    for i in d[1:]:
        ans *= e[i-1]
        ans %= MOD
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
import sys
MOD = 998244353


def main():
    n, *d =  map(int, sys.stdin.buffer.read().split())
    D = Counter(d)
    if D[0] != 1:
        print(0)
    else:
        ans = 1
        for i in d[1:]:
            ans = ans*D[i-1]%MOD
        print(ans % MOD)


if __name__ == '__main__':
    main()

Solution (Python):
MOD = 998244353

N = int(input())
D = list(map(int,input().split()))

cnt = [0] * N
for i in range(N):
	cnt[D[i]] += 1

if D[0] != 0 or cnt[0] > 1:
	print(0)
	exit()

ans = 1
for i in range(1, max(D) + 1):
	ans = (ans * pow(cnt[i - 1], cnt[i], MOD)) % MOD
print(ans)

Solution (Python):
n = int(input())
d = {}
mod = 998244353
max_v = 0
lis = list(map(int, input().split()))


if lis[0] != 0:
    print(0)
    exit()

for v in lis:
    if v not in d:
        d[v] = 1
    else:
        d[v] += 1
    max_v = max(max_v, v)


if 0 not in d or d[0] > 1:
    print(0)
    exit()

ans = 1

for i in range(1, max_v + 1):
    if i not in d or i - 1 not in d:
        ans *= 0
    else:
        ans *= pow(d[i - 1], d[i], mod)
        ans %= mod
print(ans)

Solution (Python):
def pow_r(x,n,p):
  if n == 0:
    return 1
  if n % 2 == 0:
    return pow_r(x**2 % p, n//2, p)
  else:
    return x * pow_r(x**2 % p, n//2, p) % p
    

N = int(input())
Ds = list(map(int, input().split()))
memo = [0]*N
P = 998244353

for d in Ds:
  memo[d] += 1
  
if Ds[0] != 0 or memo[0] != 1:
  print(0)
  exit()
  
rlt = 1
cnt = 1

for i in range(1,N):
  if memo[i] > 0:
    rlt *= pow_r(memo[i-1], memo[i], P)
    rlt %= P
    cnt += memo[i]
  else:
    if cnt == N:
      print(rlt)
      exit()
    else:
      print(0)
      exit()
      
print(rlt)

Solution (Python):
def main():
    n = int(input())
    d = list(map(int,input().split()))
    mod = 998244353
    if d[0]!=0:
        print(0)
        return
    D = {0:1}
    for i in range(1,n):
        if d[i]==0:
            print(0)
            return
        if D.get(d[i]) == None:
            D[d[i]] = 1
        else:
            D[d[i]] += 1
    ans = 1
    if sorted(D.keys())[-1]!=len(D.keys())-1:
        print(0)
        return
    for k in range(1,len(D.keys())):
        ans *= pow(D[k-1],D[k],mod)
        ans = ans % mod
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):

from collections import Counter
MOD = 998244353


def solve(n,d):
    if d[0] != 0:
        return 0
    dc = Counter(d)
    if dc[0] > 1:
        return 0
    res = 1
    k = 1
    dmax = max(d)
    while k <= dmax:
        res *= dc[k-1]**dc[k]
        res %= MOD
        k += 1
    return res


def main():
    N = int(input())
    d = list(map(int,input().split()))
    
    print(solve(N,d))


if __name__ == '__main__':
  main()

Solution (Python):
n = int(input())
d = list(map(int, input().split()))

a = [0] * n
for i in range(n):
    a[d[i]] += 1

if a[0] == 1 and d[0] == 0:
    ans = 1
    for i in range(1, n):
        ans *= a[i-1]**a[i]
        ans %= 998244353

    print(ans)
else:
    print(0)

Solution (Python):
import sys
import math
import itertools
import collections
from collections import deque
from collections import defaultdict

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
MOD2 = 998244353
input = lambda: sys.stdin.readline().strip()

NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()

def main():
    N = NI()
    D = NLI()
    
    if D[0] != 0:
        print(0)
        sys.exit()
    else:
        Dc = sorted(collections.Counter(D).items())
        
        if Dc[0] != (0,1):
            print(0)
            sys.exit()
        elif Dc[1][0] != 1:
            print(0)
            sys.exit()
        else:
            ans = 1
            for i in range(2,len(Dc)):
                if Dc[i][0] != i:
                    print(0)
                    sys.exit()
                else:
                    ans = (ans * ((Dc[i-1][1] **Dc[i][1]))%MOD2)%MOD2
            print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
    exit()
D = Counter(D)
if 0 not in D.keys():
    print(0)
    exit()
if D[0] > 1:
    print(0)
    exit()
max_key = max(D.keys())
cnt = [0] * (max_key + 1)
for i in range(0, max_key + 1):
    cnt[i] = D[i]
ans = 1
for i in range(1, len(cnt)):
    ans *= (cnt[i-1] ** cnt[i])
    ans %= 998244353
print(ans % 998244353)

Solution (Python):
from collections import Counter
n = int(input())
d = list(map(int, input().split()))
mod = 998244353
if d[0] != 0:
    print(0)
else:
    a = Counter(d)
    ans = 1
    if a[0] > 1:
        print(0)
    else:
        for i in range(1, max(d)+1):
            ans *= a[i-1]**a[i]
            ans %= mod
        print(ans)

Solution (Python):
from collections import*
n=input()
a=list(map(int,input().split()))
d=dict(Counter(a))
mod=998244353
ans=a[0]==0 and d[0]==1
for i in d:
  if i==0:continue
  ans*=pow(d.get(i-1,0),d[i],mod)
  ans%=mod
print(ans)

Solution (Python):
import math
import sys

n=int(input())

d=list(map(int,input().split()))

if d[0]!=0 or 0 in d[1:]:
    print(0)
    sys.exit()

lis=[0 for i in range(n)]

for i in range(n):
    lis[d[i]]+=1

for i in range(1,n-1):
    if lis[i]==0 and lis[i+1]!=0:
        print(0)
        sys.exit()
s=1

i=0
while i+1<=n-1 and lis[i+1]!=0:
    s=(s*pow(lis[i],lis[i+1],998244353))%998244353
    i+=1
print(s)

Solution (Python):
MOD = 998244353
N = int(input())
D = list(map(int, input().split()))
D_2 = [0]*(max(D)+1)
for i in D:
    D_2[i] += 1
ans = 1 if D_2[0] == 1 and D[0] == 0 else 0
for i in range(1,max(D)+1):
    ans *= D_2[i-1]**D_2[i]
    ans %= MOD
print(ans)

Solution (Python):
MOD = 998244353
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
    exit()
D_2 = [0]*(max(D)+1)
for i in D:
    D_2[i] += 1
if D_2[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(1,max(D)+1):
    ans *= D_2[i-1]**D_2[i]
    ans %= MOD
print(ans)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
mx=max(d)
l=[0]*(10**5)
mx=0
for i in range(n):
    if (i==0 and d[i]!=0) or (i!=0 and d[i]==0):
        print(0)
        exit()
    l[d[i]]+=1
    mx=max(mx,d[i])
t=1
ans=1
for i in range(1,mx+1):
    ans *= t**l[i]
    t=l[i]

print(ans%998244353)

Solution (Python):
N = int( input() )
D = list ( int(x) for x in input().split() )

if D[0] != 0:
    ans = 0
else:
    Ds = [ 0 for i in range( max(D) + 1 ) ]

    for i in range( N ):
        Ds[ D[i] ] += 1

    ans = 1

    if Ds[ 0 ] != 1:
        ans = 0
    else:
        for i in range( 1 , max(D) + 1 ):
            ans *= Ds[ i-1 ] ** Ds[i]

            if ans == 0:
                break


print( ans % 998244353 )

Solution (Python):
import collections


def main():
    n = int(input())
    D = list(map(int, input().split()))

    mod = 998244353

    if D[0] != 0:
        print(0)
        return

    cnt = collections.Counter(D)

    if cnt[0] > 1:
        print(0)
        return

    ans = 1

    for i in range(1, max(D)+1):
        if cnt[i - 1] == 1:
            continue
        ans *= cnt[i - 1] ** cnt[i] % mod
        ans %= mod

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys, math
from functools import lru_cache
from collections import defaultdict
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    D = list(mi())
    ans = 1

    if D[0] != 0:
        print(0)
        return

    dic = defaultdict(int)
    for i in range(N):
        dic[D[i]] += 1

    for i in range(1, N):
        ans *= dic[D[i]-1]
        ans %= 998244353

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
N=int(input())
D=list(map(int,input().split()))
mod=998244353

if D[0]!=0:
    print(0)
    exit()


B=[0]*N
for d in D:
    B[d]+=1
if B[0]!=1:
    print(0)
    exit()

ans=1
total=1
for i in range(1,N):
    if B[i]==0:
        if total!=N:
            print(0)
            exit()
        else:break
    total+=B[i]
    ans*=(B[i-1]**B[i])
    ans%=mod

print(ans)

Solution (Python):
N = int(input())
D = list(map(int, input().split()))
dp = [0] * N
for i in D:
  dp[i] += 1
ans = 1 if dp[0] == 1 and D[0] == 0 else 0
for i in range(1, N):
  ans *= dp[i-1] ** dp[i]
  ans %= 998244353
print(ans)

Solution (Python):
n = int(input())
D = list(map(int, input().split()))
MOD = 998244353

cnt = [0] * n
for d in D:
    cnt[d] += 1

if D[0] == 0 and cnt[0] == 1:
    res = 1
else:
    res = 0

for i in range(1, n):
    res *= pow(cnt[i - 1], cnt[i], MOD)
    res %= MOD

print(res)

Solution (Python):
p=998244353
n=int(input())
d=list(map(int,input().split()))
dep = [0]*n
for x in d:
  dep[x] += 1

ans = 1 if d[0]==0 and dep[0]==1 else 0
for i in range(1,n):
  ans *= (dep[i-1]%p) ** dep[i] % p
  ans %= p

print(ans)

Solution (Python):
import sys
from collections import defaultdict

input = sys.stdin.readline
P = 998244353


def main():
    N = int(input())
    D = list(map(int, input().split()))

    count = defaultdict(int)
    for d in D:
        count[d] += 1

    if not(D[0] == 0 and count[0] == 1):
        print(0)
        exit()

    max_depth = max(count.keys())
    ans = 1
    for d in range(max_depth):
        ans = (ans * pow(count[d], count[d + 1], mod=P)) % P

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def resolve():
    import math
    n=int(input())
    d=list(map(int,input().split()))
    d2=[0]*n
    c=1
    for m in d:
        d2[m]+=1

    
    for i in range(1,n):
        c=c* pow(d2[i-1],d2[i],998244353)

    if d2[0]>1 or d[0]!=0:
        print(0)
    else:
        print(c % 998244353)
resolve()

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf,comb
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
from functools import reduce
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n = INT()
d = LIST()
mod = 998244353

if d[0] != 0:
    print(0)
    exit()

c = Counter(d)
counts = [0]*(max(d)+1)
for k, v in c.items():
    counts[k] = v
if counts[0] > 1:
    print(0)
    exit()
ans = 1
for i in range(1, len(counts)):
    ans *= pow(counts[i-1], counts[i])
    ans %= mod

print(ans)

Solution (Python):
N=int(input())
D=list(map(int,input().split()))
M=998244353

P=max(D)
L=[0]*(P+1)
for d in D:
    L[d]+=1

if not(L[0]==1 and D[0]==0):
    print(0)
    exit()

K=1
for i in range(P):
    K*=pow(L[i],L[i+1],M)
    K%=M
print(K)

Solution (Python):

from collections import Counter
import collections
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


n = I()
d = LI()
mod = 998244353
if d[0] != 0:
    print(0)
    exit(0)




c = Counter(d)
counts = [0] * (max(d)+1)
for k, v in c.items():
    counts[k] = v
if counts[0] > 1:
    print(0)
    exit(0)
ans = 1
for i in range(1, len(counts)):
    ans *= pow(counts[i-1], counts[i])
    ans %= mod

print(ans)

Solution (Python):

from collections import Counter

N = int(input())
X = list(map(int, input().split()))

MOD = 998244353
ctr = Counter(X)

if X[0] == 0 and ctr[0] == 1:
    ans = 1
    for i in range(1, max(X) + 1):
        ans *= pow(ctr[i - 1], ctr[i], MOD)
        ans %= MOD
    print(ans)
else:
    print(0)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
d = list(map(int, input().split()))
M = 998244353
from collections import Counter
c = Counter(d)
if d[0]!=0 or c[0]>=2:
    ans = 0
else:
    m = max(c.keys())
    ans = 1
    for i in range(1,m+1):
        if i not in c:
            ans = 0
            break
        ans *= pow(c[i-1], c[i], M)
        ans %= M
print(ans%M)

Solution (Python):
from collections import Counter

def solve():
    N = int(input())
    D = list(map(int, input().split()))

    mod = 998244353

    if D[0] != 0:
        print(0)
        return

    cnt = Counter(D)

    if cnt[0] > 1:
        print(0)
        return

    res = 1

    for i in range(1, max(D)+1):
        if cnt[i-1] == 1:
            continue
        res *= cnt[i-1]**cnt[i] %mod
        res %= mod
    
    print(res)

solve()

Solution (Python):


from fractions import gcd

from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

ans = lambda x : x % 998244353
MOD = 998244353

N = int(input())
D = [int(_) for _ in input().split()]

c = Counter(D)

ans = 1 * (not D[0]) * (c[0] == 1)
for i in range(1, max(c.keys())+1):
    ans *= pow(c[i-1], c[i], MOD)
    ans %= MOD

print(ans)

Solution (Python):
from collections import defaultdict
import sys
input = sys.stdin.buffer.readline


def mod_pow(x, n, mod=10**9 + 7) -> int:
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
            K %= mod
        x *= x
        x %= mod
        n //= 2

    return (K * x) % mod


MOD = 998244353

n = int(input())
D = [int(i) for i in input().strip().split()]

_max = max(D)

counts = defaultdict(int)

if D[0] != 0:
    print(0)
    sys.exit()

for d in D:
    counts[d] += 1

if counts[0] != 1:
    print(0)
    sys.exit()

ans = 1
for i in range(1, _max + 1):
    if i not in counts.keys():
        print(0)
        sys.exit()
    elif i == 1:
        continue
    else:
        x = counts[i - 1]
        k = counts[i]
        ans = (ans * mod_pow(x, k, MOD)) % MOD

print(ans)

Solution (Python):
from collections import defaultdict

N = int(input())
D = list(map(int, input().split()))

MOD = 998244353

p = defaultdict(int)
for d in D:
    p[d] += 1

if p[0] == 1 and D[0] == 0:
    ans = 1
    for i in range(max(D)):
        ans *= pow(p[i], p[i + 1], MOD)
        ans %= MOD
        if ans == 0:
            break

    print(ans)

else:
    print(0)

Solution (Python):
from collections import defaultdict

N = int(input())
D = list(map(int, input().split()))

MOD = 998244353

p = defaultdict(int)
for d in D:
    p[d] += 1

if p[0] == 1 and D[0] == 0:
    ans = 1
    for i in range(max(D)):
        ans *= pow(p[i], p[i + 1], mod=MOD)
        ans %= MOD
        if ans == 0:
            break

    print(ans)

else:
    print(0)

Solution (Python):
from collections import defaultdict

N = int(input())
D = list(map(int, input().split()))

MOD = 998244353

p = defaultdict(int)
for d in D:
    p[d] += 1

if p[0] == 1 and D[0] == 0:
    ans = 1
    for i in range(max(D)):
        ans *= p[i] ** p[i + 1]
        ans %= MOD
        if ans == 0:
            break

    print(ans)

else:
    print(0)

Solution (Python):
from collections import defaultdict

N = int(input())
D = list(map(int, input().split()))

MOD = 998244353

p = defaultdict(int)
for d in D:
    p[d] += 1

if p[0] == 1 and D[0] == 0:
    ans = 1
    for i in range(N + 1):
        ans *= p[i] ** p[i + 1]
        ans %= MOD

    print(ans % MOD)

else:
    print(0)

Solution (Python):
from collections import defaultdict

N = int(input())
D = list(map(int, input().split()))

MOD = 998244353

p = defaultdict(int)
for d in D:
    p[d] += 1

if p[0] == 1 and D[0] == 0:
    ans = 1
    for i in range(N + 1):
        ans *= p[i] ** p[i + 1]
        ans %= MOD

    print(ans % MOD)

else:
    print(0)

Solution (Python):
def my_pow(base, n, mod):
    if n == 0:
        return 1
    x = base
    y = 1
    while n > 1:
        if n % 2 == 0:
            x *= x
            n //= 2
        else:
            y *= x
            n -= 1
        x %= mod
        y %= mod
    return x * y % mod

N = int(input())
D = list(map(int, input().split()))
dmax = max(D)
MOD = 998244353
cnt = [0] * (10 ** 5 + 1)
for d in D:
    cnt[d] += 1

if D[0] or cnt[0] != 1:
    print(0)
    exit()
ans = cnt[0]
for i in range(1, dmax + 1):
    now = my_pow(cnt[i - 1], cnt[i], MOD)
    ans *= now
    ans %= MOD
print(ans)