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):
N = int(input())
D = list(map(int, input().split()))

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

length = 1
length_list = []
count = 0
for i in range(len(D)):
    if length == D[i]:
        count += 1
    else:
        if length+1 != D[i]:
            print(0)
            exit()
        else:
            length += 1
            length_list.append(count)
            count = 1
length_list.append(count)
        
ans = 1
for i in range(len(length_list)-1):
    ans *= length_list[i] ** length_list[i+1]
    ans %= 998244353


print(ans)

Solution (Python):
N = int(input())
D = list(map(int,input().split()))
d = { }
for i in range(N+1):
    d[i] = 0
for i in range(N):
    x = D[i]
    d[x] += 1
p = 0
if D[0] == 0:
    p = 1
q = 0
if d[0] == 1:
    q = 1
M = max(D)
r = 1
e = 998244353
for i in range(1,M):
    x = d[i]
    y = d[i+1]
    r = r*(x**y) % e
ans = p*q*r
print(ans)

Solution (Python):
N=int(input())
D=list(map(int,input().split()))
dp=[0]
x,r=0,1
m=998244353
if D[0]!=0:
    r=0
D.sort()
for d in D:
    if d==x:
        dp[x]+=1
    elif d==x+1:
        dp.append(1)
        x+=1
    else:
        r=0
    r=(r*dp[x-1])%m
print(r%m if dp[0]==1 else 0)

Solution (Python):
import collections
import sys


def solve():
    input = sys.stdin.readline
    mod = 998244353
    n = int(input().rstrip('\n'))
    d = list(map(int, input().rstrip('\n').split()))
    mn = 0
    dic = collections.defaultdict(list)
    for i in range(n):
        dic[d[i]] += [i+1]
        mn = max(mn, d[i])
    if len(dic[0]) == 1 and d[0] == 0:
        ln = 1
        t = 1
        for i in range(1, mn+1):
            if len(dic[i]) != 0:
                t *= ln ** len(dic[i])
                t %= mod
                ln = len(dic[i])
            else:
                print(0)
                exit()
        print(t)
    else:
        print(0)
        exit()


if __name__ == '__main__':
    solve()

Solution (Python):



import sys
from collections import Counter


input = sys.stdin.readline
MOD = 998244353


def main():
    N = int(input())
    D = [int(i) for i in input().split()]
    c = Counter(D)
    k = list(c.keys())
    k.sort()
    if D[0] != 0 or c[0] != 1:
        print(0)
        sys.exit(0)

    ans = 1
    for i, n in enumerate(k):
        if i != n:
            print(0)
            sys.exit(0)
        if n == 0:
            continue
        ans *= c[n-1]**c[n]

    print(ans % MOD)


if __name__ == '__main__':
    main()

Solution (Python):
import collections

import sys
big=998244353

n = int(input())
d = list(map(int, input().split()))
d_sort = sorted(d)


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

for i in range(n-1):
  if d_sort[i+1]-d_sort[i]>1:
    print(0)
    sys.exit(0)


def cho(node0,node1):
  
  
  
  
  return (node0**node1) % big


c = collections.Counter(d)

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

count=1
for i in range(max(d)):
  count *= cho(c[i], c[i+1])
  
  count=count%big

print(count)

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

if e[0] != 0:
  ans = 0
else:
  ce = Counter(e)
  sce = sorted(ce.items())
  
  if sce[-1][0] != len(sce) - 1 or sce[0][1] != 1:
    ans = 0
  else:
    ans = 1
    pre = 1
    for i, j in sce:
      if pre != 1:
        ans = (ans * (pre ** j)) % 998244353
      pre = j
      
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

_n = int(input())
_dlist = list(map(int, input().split()))


def chker(n, dlist):
    if dlist[0] != 0:
        print(0)
        return

    ds = dlist[1:]
    ds.sort()
    cntlist = [0] * n
    if ds[0] != 1:
        print(0)
        return

    cntlist[ds[0]] = 1
    for i in range(1, n - 1):
        if ds[i] <= ds[i - 1] + 1:
            cntlist[ds[i]] += 1
        else:
            print(0)
            return

    ans = 1
    for i in range(2, n):
        ans = (ans * cntlist[i - 1] ** cntlist[i]) % 998244353

    print(ans)


chker(_n, _dlist)

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

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

D.sort()

chouten = {}
for i in D:
    if i not in chouten:
        chouten[i] = 0
    chouten[i] += 1



if 0 not in chouten or chouten[0] != 1:
    print(0)
    exit()

kosuu = 1






for i in range(len(chouten)):
    if i not in chouten:
        print(0)
        exit()
    if i == 0:
        continue
    elif i == 1:
        continue
        kosuu = chouten[i]
    else:
        kosuu *= chouten[i - 1]**chouten[i]
        kosuu = kosuu % 998244353

print(kosuu)

Solution (Python):
n= int(input())

dlist = list(map(int,input().split()))
lendlist = len(dlist)
ans = -1
dictdist = {}

for i in range(100001):
    dictdist[i] = 0

for i in range(lendlist):
    if i==0:
        if dlist[i]!=0:
            ans = 0
            
    elif i!=0:
        if dlist[i]==0:
            ans = 0
        else:
            dictdist[dlist[i]] += 1
                
if ans != 0:
    temp = 1
    for k,v in dictdist.items():
        if k != 1:
            for _ in range(v):
                temp *= dictdist[k-1]
                temp %= 998244353
                
    ans = temp
    
print(ans)

Solution (Python):
from collections import Counter 

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

answer = 1
prev = 1
count = Counter(D)
for i in range(max(count.keys()) + 1):
    if D[0] != 0 or (i == 0 and count[i] != 1):
        answer = 0
        break
    if i not in count:
        answer = 0
        break
    answer *= prev ** count[i]
    prev = count[i]
print(answer % 998244353)

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

N = int(input().strip())
D = [int(x) for x in input().strip().split()]
maxD = max(D)
C = [0] * (maxD + 1)
for d in D:
    C[d] += 1
ans = 1
c_ = 1
if C[0] != 1 or D[0] != 0:
    ans = 0
else:
    for i, c in enumerate(C[1:], start=1):
        ans *= c_ ** c
        ans %= 998244353
        c_ = c

print(ans)

Solution (Python):
import collections

n = int(input())
d_list = list(map(int, input().split()))

if d_list[0] != 0:
  print(0)
elif 0 in d_list[1:]:
  print(0)
else:
  d_count_dict = collections.Counter(d_list)
  start_num = -1
  answer = 1
  last = 1
  for num, cnt in d_count_dict.items():
    if num != start_num + 1:
      answer = 0
    else:
      answer *= ((last ** cnt)%998244353)
      last = cnt
      start_num = num
  print(answer % 998244353)

Solution (Python):
n = int(input())
d = list(map(int,input().split()))
ans = 1
count1 = 1
count = 0
now = 0
mod = 998244353
if d[0] != 0:
    print(0)
else:
    d.sort()
    for i in range(1,n):
        if d[i] == now+1:
            count += 1
        else:
            if count == 0 or d[i] == 0:
                ans = 0
                break
            elif d[i] > now + 2:
                ans = 0
                break
            else:
                p = 1
                for j in range(count):
                    p *= count1
                    p %= mod
                ans *= p
                ans %= mod
                count1 = count
                count = 1
                now += 1
        
    p = 1
    for i in range(count):
        p *= count1
        p %= mod
    ans *= p
    ans %= mod
    print(ans)

Solution (Python):
n = int(input())
D = list(map(int,input().split()))
mod = 998244353
if D[0]!= 0:
  print(0)
else:
  D.sort()
  if D[0]==0 and D[1]==0:
    print(0)
  elif D[0] ==0 and D[1] != 1:
    print(0)
  else:
      D += [max(D)+1]
      ans = 1
      before = 1
      now = 1
      ok =True
      for i in range(1,n):
          if D[i]==D[i+1]:
              now +=1
          else:
            if D[i+1] == D[i]+1:
                ans *= ((before%mod)**now)%mod
                before = now
                now = 1
                ans = ans%mod
            else:
              print(0)
              ok = False
              break
      if ok:
        print(ans)

Solution (Python):
n = int(input())
d = [int(i) for i in input().split(" ")]
if d[0]!=0:
    print(0)
else:
    te = [1] + [0 for i in range(max(d))]
    co = [0 for i in range(max(d)+1)]
    for i in d:
        co[i] += 1
    if 0 in co[1:]:
        print(0)
    elif co[0] != 1:
        print(0)
    elif len(co)==2:
        print(1)
    else:
        for i in range(1,len(co)):
            te[i] = te[i-1]*(co[i-1]**co[i]) % 998244353

        print(te[-1])

Solution (Python):
N = int(input())
Ds  = list(map(int, input().split()))
if Ds[0] != 0:
    print(0)
    exit()
Ds.sort()

mod = 998244353

from collections import defaultdict
d = defaultdict(int)

d_max = Ds[-1]
for D in Ds:
    d[D] += 1

ans = 1

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

Solution (Python):
import sys


n = int(input())
d = list(int(x) for x in input().split())

if d[0] != 0:
    print(0)
    sys.exit()
d.sort()
ans = 1
key = 1
value = 1
for i in range(1, n):
    if i == 1 and d[i] != 1:
        print(0)
        sys.exit()
    else:
        pass
    if d[i] == d[i-1]:
        key += 1
    else:
        if d[i] == d[i-1] + 1:
            if d[i-1] == 1:
                value = key
                key = 1
            else:
                ans *= pow(value, key)%998244353
                value = key
                key = 1
        else:
            print(0)
            sys.exit()
t = pow(value, key)
ans = ans*t%998244353
print(ans)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys
import bisect
import collections

mod = 998244353
sys.setrecursionlimit(mod) 

def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
D = LI()

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

if 0 in D[1:]:
    print(0)
    sys.exit()


D.sort()
c = collections.Counter(D)
ans = 1
parents = 1
before = -1
for key in c.keys():
    if before + 1 != key:
        print(0)
        sys.exit()
    ans *= parents**c[key]
    ans = ans % mod
    parents = c[key]
    before = key

print(ans)

Solution (Python):
from collections import Counter

def read():
    N = int(input().strip())
    D = list(map(int, input().strip().split()))
    return N, D

def argsort(x):
    pairs = [(i, x[i]) for i in range(len(x))]
    pairs.sort(key=lambda a: a[1])
    return [a[0] for a in pairs]


def solve(N, D, P=998244353):
    D = [-1] + D
    count = Counter(D)
    count_max = max(count.keys())

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

    result = 1
    for i in range(1, count_max + 1):
        if count[i] == 0:
            return 0
        n_childs = count[i]
        n_parents = count[i-1]
        ptn = n_parents ** n_childs
        result *= ptn
        result %= P
    return result

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Solution (Python):
import sys
input = sys.stdin.readline

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

if 0 in D[1:] or D[0] != 0:
    print(0)
    exit(0)
else:
    num = [0]*max(D)
    for i in range(1, N):
        if D[i] > 0:
            num[D[i]-1] += 1

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

    print(ans % mod)

Solution (Python):

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

cnt = dict()
for k in d:
    if k in cnt:
        cnt[k] += 1
    else:
        cnt[k] = 1

mod = 998244353

keys = cnt.keys()
pre = -1
rst = 1

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


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


for k in sorted(keys):
    if (pre + 1) != k:
        rst = 0
        break
    pre = k

    if k == 0:
        continue

    rst = (rst * (cnt[k-1] ** cnt[k])) % mod
    
print(rst)

Solution (Python):

import sys
read = sys.stdin.readline


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_matrix(H):
    '''
    H is number of rows
    '''
    return [list(map(int, read().split())) for _ in range(H)]


def read_map(H):
    '''
    H is number of rows
    文字列で与えられた盤面を読み取る用
    '''
    return [read()[:-1] for _ in range(H)]


def read_col(H, n_cols):
    '''
    H is number of rows
    n_cols is number of cols

    A列、B列が与えられるようなとき
    '''
    ret = [[] for _ in range(n_cols)]
    for _ in range(H):
        tmp = list(map(int, read().split()))
        for col in range(n_cols):
            ret[col].append(tmp[col])

    return ret


N = read_a_int()
D = read_ints()

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























from collections import Counter
dic = Counter(D)
ans = 1

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

for i in range(max(D)):
    
    try:
        dic[i + 1]
    except:
        print(0)
        exit()

    ans *= dic[i]**dic[i + 1]
    if ans > 998244353:
        ans = ans % 998244353

print(ans)

Solution (Python):
n=int(input())
ds=[int(i) for i in input().split()]
counts={}

if ds[0]!=0:
  ans=0

else:  
    for i in range(1,n):
      if ds[i] not in counts:
        counts[ds[i]]=1
      else:
        counts[ds[i]]+=1
    c=0
    for i in range(1,len(counts)+1):
      if i in counts:
        c+=1
    if c==len(counts):
      ans=1
      for i in range(1,len(counts)):
          x=(counts[i]**counts[i+1])%998244353
          ans*=x
          ans%998244353
    else:
      ans=0

print(ans%998244353)

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

Solution (Python):
mod=998244353

N=int(input())
L=[0 for i in range(N)]
D=list(map(int,input().split()))

if D[0]!=0:
    print(0)
    exit()
for i in range(N):
    L[D[i]]+=1


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

ans=1
cnt=1
for i in range(1,N):
    ans*=pow(L[i-1],L[i],mod)
    cnt+=L[i]
    
    if cnt==N:
        break
print(ans%mod)

Solution (Python):
n=int(input())
D=list(map(int,input().split()))
T=[[] for _ in range(max(D)+1)]
for i in range(n):
    T[D[i]].append(i+1)
if T[0]!=[1]:
    print(0)
    exit()
a=1
for j in range(1,len(T)):
    if T[j]==[]:
        print(0)
        exit()
    else:
        a*=len(T[j-1])**len(T[j])
        a%=998244353
print(a)

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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)
mod = 998244353
ans = 1



n = int(readline())
d = list(map(int, readline().split()))

import collections
d_count = collections.Counter(d)

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

for i in range(n):
     ans = ans * (d_count[i] ** d_count[i+1]) % mod
     
     if d_count[i] == 0 and d_count[i+1] != 0:
         print(0)
         sys.exit()
print(ans)

Solution (Python):
N = int(input())
D = list(map(int, input().split()))
if (D[0]!=0) or (0 in D[1:]):
  print(0)
else:
  mapping = dict()
  max_ = 0
  for n in D[1:]:
    if n in mapping:
      mapping[n]+=1
    else:
      mapping[n]=1
      if max_ < n :
        max_ = n
  ans=1
  for i in range(1,max_):
    if (i in mapping) and (i+1 in mapping):
      ans *= mapping[i]**(mapping[i+1])
    else:
      ans = 0
      break
  print(ans % 998244353)

Solution (Python):
from collections import Counter

MOD = 998244353

N = int(input())
D = [int(_) for _ in input().split()]
if D[0] != 0:
    print(0)
    exit()

counter = Counter(D)
if counter[0] != 1:
    print(0)
    exit()
max(D)
cnt = 0
ans = 1
pre = 1

for i in range(1, max(D) + 1):
    if counter[i] == 0:
        print(0)
        exit()
    ans *= (pre ** counter[i]) % MOD
    ans = ans % MOD
    pre = counter[i]
print(ans)

Solution (Python):

import sys
from collections import Counter
MOD = 998244353  

def solve(N: int, D: "List[int]"):
    if D[0] != 0:
        print(0)
        return 
    
    for i in range(1,N):
        if D[i]==0:
            print(0)
            return 

    
    D.sort()
    counter = Counter(D).items()
    answer = 1
    prev_node_count = 1
    layer = 0
    for c in counter:
        if layer == c[0]: 
            answer*=prev_node_count**c[1]
            prev_node_count = c[1]
            layer+=1
        else:
            print(0)
            return 
    
    print(answer%MOD)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    D = [int(next(tokens)) for _ in range(N)]  
    solve(N, D)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from collections import Counter

n = int(input())
d_ls = [int(i) for i in sys.stdin.readline().split()]
ct = Counter(d_ls)
res = 1
if d_ls[0] != 0 or ct[0] > 1:
    print(0)
else:
    for i, count in sorted(ct.items(), key=lambda x:x[1]):
        if i == 0:
            continue
        res = (res * (ct[i-1] ** count)) % 998244353
    print(res)

Solution (Python):
import collections
import sys
sys.setrecursionlimit(10**6+10)
N=int(input()) 
d=list(map(int, input().split())) 

  
  
c = collections.Counter(d)
c = dict(collections.Counter(d))

if d[0] != 0 or c[0]>1:
  print(0)
  sys.exit()
max_key = max(c.keys())


count = 1
for i in range(2,max_key+1):
  a = c.get(i, -1)
  b = c.get(i-1, -1)
  if a == -1 or b == -1:
    print(0)
    sys.exit()
  count = (count * (b**a))%998244353

print(count)

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

ans = 1
if d[0] == 0 and d.count(0) == 1:
    l = [0 for i in range(10**5+10)]
    for i in range(n):
        l[d[i]] += 1
    
    count = l[0] + l[1]
    for i in range(2,10**5+10):
        if l[i] == 0:
            break
        else:
            ans = ans * l[i-1]**l[i]
   
            count += l[i]
    if count != n:
        ans = 0
    print(ans%998244353)
    
else:
    print(0)

Solution (Python):
import collections

input = raw_input
range = xrange
M = 998244353

def main():
    N = int(input())
    Ds = list(map(int, input().split(" ")))
    if Ds[0] != 0:
        return 0

    counter = collections.Counter(Ds)
    depths = counter.keys()
    
    min_depth = min(depths)
    max_depth = max(depths)

    if min_depth != 0:
        
        return 0
    elif counter[0] != 1:
        return 0
    
    if len(depths) != max_depth + 1:
        
        return 0

    out = 1
    for depth in range(1, max_depth+1):
        num = counter[depth]
        if depth >= 2:
            for i in range(num):
                out = out * before_num
                out = out % M
        before_num = num

    return out

out = main()
print(out)

Solution (Python):
from collections import Counter

n = int(input())
d = list( map(int, input().split()))
c=Counter(d)
dmax = max(d)
MOD = 998244353

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

if len(c)-1 != dmax:
    print(0)
    exit()

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

ans = 1

for i in range(1,dmax):
    ans *= c[i]**c[i+1]
    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.sort()
before_cnt = 1
tmp = 1
cnt = 0
ans = 1

for i in range(1, len(D)):
    
    if (tmp == D[i]):
        cnt += 1
    else:
        if tmp + 1 != D[i]:
            
            print(0)
            exit()
        ans *= before_cnt ** cnt
        tmp = D[i]
        before_cnt = cnt
        cnt = 1

ans *= before_cnt ** cnt
print(ans % mod)

Solution (Python):
N = int(input())
D = list(map(int,input().split()))
c = [0 for i in range(N)]
if D[0] != 0:
	print(0)
	exit()
for d in D:
	c[d] += 1
MOD = 998244353

if c[0] != 1:
	print(0)
	exit()
flag = True
for i in range(N):
	if not flag and c[i] > 0:
		print(0)
		exit()
	if c[i] == 0:
		flag = False

ans = 1
for i in range(1, N):
	if c[i] == 0:
		print(ans)
		exit()
	t = (c[i-1] ** c[i]) % MOD
	ans = (ans * t) % MOD
print(ans)

Solution (Python):
N = int(input())
D_list = list(map(int, input().split()))
mod = 998244353
ans = 1
count_list = [0]*(max(D_list) + 1)
if max(D_list) >= N:
    ans = 0
else:
    for i in range(len(D_list)):
        count_list[D_list[i]] += 1
    if count_list[1] == 0:
        ans = 0
    else:
        for i in range(2, len(count_list)):
            if count_list[i] == 0:
                ans = 0
                break
            ans *= count_list[i-1]**count_list[i]

if D_list[0] != 0 or count_list[0] >= 2:
    ans = 0

print(ans%mod)

Solution (Python):
mod = 998244353
n = int(input())
d = list(map(int, input().split()))
a = [0] * (10 ** 5)
if d[0] != 0 or d.count(0) != 1:
  print(0)
  exit()
for i in d:
  a[i] += 1
now = a[1]
if now == 0:
  if n == 1:
    print(1)
    exit()
  else:
    print(0)
    exit()
ans = 1
now_sum = now + 1
for i in a[2:]:
  if not i:
    if n != now_sum:
      ans = 0
    break
  else:
    ans *= pow(now, i, mod)
  now_sum += i
  now = i
print(ans % mod)

Solution (Python):
n = int(input())
d_list = list(map(int, input().split()))
divider = 998244353

if d_list[0] != 0:
  print(0)
else:
  d_sort = sorted(d_list)
  d = 1
  pre_c = 1
  cur_c = 0
  count = 1
  for i in range(1, n):
    if d_sort[i] == d:
      cur_c += 1
    elif d_sort[i] == d + 1:
      count *= pre_c ** cur_c
      count %= divider
      d += 1
      pre_c = cur_c
      cur_c = 1
    else:
      count = 0
      cur_c = 0
      break
  count *= pre_c ** cur_c
  count %= divider
  print(count)

Solution (Python):
MOD = 998244353

n = int(input())
D = list(map(int,input().split()))
if D[0]!=0:
    print(0)
    exit()
M = max(D)
L = [0]*(M+1)
for i in range(n):
    L[D[i]] += 1
if L[0]!=1:
    print(0)
else:
    ans = 1
    for i in range(1,M+1):
        ans *= L[i-1]**L[i]%MOD
    print(ans%MOD)

Solution (Python):
import math

n = int(input())
a = list(map(int, input().split()))

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



if (a[0]!=0):
    print(0)
else:
    zero = 0
    for j in range(n):
        if(a[j]==0):
            zero+=1
    
    if(zero==1):
        ans = 1
        now = 1
        total = 1
        while(total<n and now<=n-1):
            for j in range(dist[now]):
                ans *= dist[now-1]
                
                ans %= 998244353
                total+=1
            now+=1
            
        print(ans)
    else:
        print(0)

Solution (Python):
import collections
N = int(input())
D = [int(_) for _ in input().split()]
mod = 998244353
C = collections.Counter(D)
if D[0] + 1 == C[0] == 1:
    ans = 1
    for i in range(1, max(C.keys()) + 1):
        ans *= C[i - 1]**C[i]
        ans %= mod
    print(ans)
else:
    print(0)

Solution (Python):
from itertools import dropwhile

n = int(input())
D = list(map(int, input().split()))
DIV = 998244353

dist = [0]*n

for i in D:
    dist[i] += 1

dist.reverse()
ddist = list(dropwhile(lambda x: x == 0, dist))
ddist.reverse()

ans = 1
for i in range(len(ddist) - 1):
    ans *= ddist[i] ** ddist[i+1]

if ddist[0] != 1 or D[0] != 0:
    ans = 0

print(ans%DIV)

Solution (Python):
import sys
N=int(input())
D=list(map(int,input().split()))
if D[0]!=0:
  print(0)
  sys.exit()
D=sorted(D[1:])
ans=1
count1=1
count2=0
before=1
for i in D:
  if i==count1:
    count2+=1
  elif i==count1+1:
    ans = ans*(before**count2)
    count1+=1
    before=count2
    count2=1
  else:
    print(0)
    sys.exit()
  
print((ans*(before**count2))%998244353)

Solution (Python):
from collections import Counter
import sys

mod = 998244353

n = int(input())
d = [int(v) for v in input().split()]

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

if len(d) == 1:
    print(1)
    sys.exit(0)

cnt = Counter(d[1:])
keys = sorted(cnt.keys())
for i in range(len(keys)):
    if keys[i] != i+1:
        print(0)
        sys.exit(0)

tree = [cnt[k] for k in sorted(cnt.keys())]

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

print(ans)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
s=[0]*n
m=998244353
for i in d:
    s[i]+=1
if d[0]==0 and s[0]==1:
    c=1
    a=1
    for i in range(1,n):
        if s[i]!=0:
            c+=s[i]
            a=(a*pow(s[i-1],s[i],m))%m
        else:
            break
    if c==n:
        print(a%m)
    else:
        print(0)
else:
    print(0)

Solution (Python):
def main():
    n = input()
    dd = map(int, raw_input().split())
    count = {}
    for d in dd:
        count[d] = count.get(d, 0) + 1
    if count.get(0, 0) != 1 or dd[0] != 0:
        print 0
        return
    ans = 1
    pre = 0
    for c in sorted(count)[1:]:
        if pre + 1 != c:
            print 0
            return
        ans = ans * pow(count[pre], count[c], 998244353) % 998244353
        pre = c
    print ans


main()

Solution (Python):
import sys

sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def solve(inputs):
    SSSS = 998244353
    D = string_to_int(inputs[0])
    
    nodes = {}
    for i, d in enumerate(D):
        i += 1
        if d not in nodes:
            nodes[d] = [i]
        else:
            nodes[d].append(i)

    if 0 not in nodes:
        return 0

    prev = -1
    prev_num = -1
    count = 0
    for d in sorted(nodes.keys()):
        if d == 0:
            if not (len(nodes[d]) == 1 and nodes[d][0] == 1):
                return 0
            count = 1
            prev_num = 1
            prev = d
            continue
        if abs(d - prev) != 1:
            return 0

        count *= prev_num ** len(nodes[d])
        prev_num = len(nodes[d])
        prev = d
    return count % SSSS


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    input()
    ret = solve(inputs(1))
    print(ret)

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

if D[0] > 0:
    print("0")
    exit()
    
D.pop(0)
list.sort(D)
if D[0] == 0:
    print("0")
    exit()
    
hoge = 0
nyoro = 1
huga = 1
ans = 1
for i in range(N-1):
    if D[i] == hoge:
        nyoro += 1
    elif D[i] == hoge + 1:
        ans *= huga ** nyoro
        hoge += 1
        huga = nyoro
        nyoro = 1
    else:
        print("0")
        exit()
ans *= huga ** nyoro
print(ans % 998244353)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
d = defaultdict(int)
m = 0
for i in a:
    d[i] += 1
    m = max(m,i)
res = 1
tmp = 1
for i in range(m+1):
    if i == 0:
        if d[i] != 1 or a[0] != 0:
            print(0)
            quit()
        continue
    if d[i] == 0:
        res = 0
        break
    if i == 1:
        tmp = d[i]
        continue
    res = (res * tmp ** d[i] )%998244353
    tmp = d[i]
print(res%998244353)

Solution (Python):
from collections import defaultdict

def main():
    N = int(input())
    D = list(map(int, input().split()))
    MOD = 998244353
    for i, d in enumerate(D):
        if (i == 0 and d != 0) or (i > 0 and d == 0):
            print(0)
            exit()
    set_D = list(set(D))
    set_D.sort()
    cnt = 0
    for i in set_D:
        if cnt == i:
            cnt += 1
        else:
            print(0)
            exit()

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

    cnt = 1
    pre = 0
    for key, value in A.items():
        if key == 0 or key == 1:
            pre = value
        else:
            cnt *= pre ** value 
            cnt %= MOD
            pre = value
    print(cnt)

    

    


if __name__ == "__main__":
    main()

Solution (Python):
import collections

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

ans = 1
m = max(D)
c = collections.Counter(D)

if 0 not in c.keys() or (c[0] != 1 or D[0] != 0):
    print(0)
    exit()

temp = 1
for i in range(1, m+1):
    if i not in c.keys():
        print(0)
        exit()
    ans *= temp**c[i]
    temp = c[i]
    if ans > 998244353:
        ans %= 998244353  

print(ans % 998244353)

Solution (Python):
import collections
N = int(input())
D = list(map(int, input().split()))
D_C = collections.Counter(D)
D_C = D_C.items()
D_C = sorted(D_C)


N = len(D_C)
MOD = 998244353
ans = 1

if (D[0] != 0) or (D_C[0][1] != 1):
  ans = 0
  print(ans % MOD)
  quit()
  
for i in range(N-1):
  ans = ans * (D_C[i][1] ** D_C[i + 1][1])
  if i > 0:
    if D_C[i][0] != x + 1:
      ans = 0
      print(ans)
      quit()
  x = D_C[i][0]

if D_C[N-1][0] != x + 1:
  ans = 0
print(ans % MOD)

Solution (Python):
n = int(input())

mod = 998244353

d = [int(i) for i in input().split()]

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

del d[0]

d.sort()

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

ans = 0
li = []
tmp = d[0]
cnt = 1


for i in range(len(d)-1):
    
    if tmp == d[i+1]:
        cnt += 1
    else:
        li.append(cnt)
        tmp += 1
        if i+1 == len(d)-1:
            cnt = 1
            if d[i+1] != tmp:
                print(0)
                exit()
            break
        else:
            if d[i+1] != tmp:
                print(0)
                exit()
        cnt = 1

li.append(cnt)

ans = 1

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

print(ans)

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

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

MOD=998244353
max_d=max(d)
c=[0 for _ in range(max_d+1)]
for i in range(n):
    c[d[i]]+=1

ans=1
for i in range(max_d):
    ans*=c[i] ** c[i+1]
print(ans%MOD)

Solution (Python):
from collections import Counter

n = int(input())
d = list(map(int, input().split()))
if d[0] != 0:
    print(0)
    exit()
for i in range(1, n):
    if d[i] == 0:
        print(0)
        exit()
d.sort()
for i in range(n-1):
    if d[i+1] - d[i] > 1:
        print(0)
        exit()

c = sorted(Counter(d).items())
ans = 1

for i in range(len(c)-1):
    if c[i][0] == 0: continue
    ans *= c[i][1] ** c[i+1][1]

print(ans % 998244353)