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):
import collections
N = int(input())
s = list(map(int, input().split()))
item = 0
sum = 0
node = 0
count = 0
before = 0


c = collections.Counter(s)

for i in range(N):
    count = c[i]
    node += count

    if i == 0:
        if count != 1:
            break
        else:
            before = 1
            sum = 1
    else:
        if count == 0:
            break
        else:
            sum = sum * (c[i-1] ** count)
            before = count

if node != len(s) or s[0] != 0:
    print("0")
else:
    print(sum%998244353)

Solution (Python):
import collections

n=int(input())
d=list(map(int,input().split()))
if d[0]!=0:
  print(0)
elif 0 in d[1:]:
  print(0)
else:
  dic=collections.Counter(d[1:])
  sorted(dic)
  p=1
  for i in range(1,len(dic)):
    p*=dic[i]**dic[i+1]
  print(p%998244353)

Solution (Python):
import sys
import bisect

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

for i, d in enumerate(D):
    if d != 0 and i == 0:
        print(0)
        sys.exit()

    elif d == 0 and i != 0:
        print(0)
        sys.exit()

D.sort()


ans = 1
cnt = []
for d in range(N):

    cnt.append(bisect.bisect(D, d))

for d in D:
    if d == 0 or d == 1:
        continue

    ans = ans * (cnt[d - 1] - cnt[d - 2]) % 998244353


print(ans)

Solution (Python):


import sys
import math

debug = False

def log(text):
    if debug:
        print(text)

def parse_input(lines_as_string = None):

    global debug
    lines = []
    if lines_as_string is None:
        debug = False
        
        
        lines.append(input())
        lines.append(input())
    else:
        debug = True
        lines = [e for e in lines_as_string.split("\n")][1:-1]

    n = [int(e) for e in lines[0].split(" ")][0]
    d = [int(e) for e in lines[1].split(" ")]

    return (n, d)


def solve(n, d):

    if d[0] != 0:
        return 0 
     
    d.sort()

    z = [] 
    tmp = d[0]
    count = 0
    for i in range(n):
        if tmp != d[i]:
            z.append((tmp, count))
            tmp = d[i]
            count = 1
        else:
            count = count + 1
    z.append((tmp, count))

    if z[0][0] != 0 or z[0][1] > 1:
        return 0

    result = 1
    m = len(z)
    for i in range(1, m):
        if z[i-1][0] + 1 != z[i][0]:
            result = 0
            break
        else:
            result = result * z[i-1][1] ** z[i][1]
    
    res = result % 998244353

    return res


def main():
    
    result = solve(*parse_input())
    if isinstance(result, list):
        for r in result:
            print("%s" % r, sep='')
    else:
        print("%s" % result, sep='')

if __name__ == '__main__':

    main()

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

Solution (Python):


N = int(input())

dList = [int(i) for i in input().split()]
ispossible = True

if dList[0] != 0:
    ispossible = False
dList.pop(0)

amari = 998244353
dList.sort()

if dList[0] <= 0:
    ispossible = False


sameDistanceCount = 1
prevdistance = 0
branch = 1
count = 1

for i in range(N-1):
    nowdistance = dList[i]
    if nowdistance - prevdistance >= 2:
        ispossible = False
        break
    elif nowdistance == prevdistance + 1:
        for j in range(sameDistanceCount):
            count *= branch
            count %= amari
        branch = sameDistanceCount
        prevdistance = nowdistance
        sameDistanceCount = 1
    else:
        sameDistanceCount += 1


for i in range(sameDistanceCount):
    count *= branch
    count %= amari

if ispossible:
    print(count)
else:
    print(0)

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

boxes = [[] for i in range(N)]
impossible = False
for idx,num in enumerate(ls):
    if num>=N:
        impossible=True
        break
    boxes[num].append(idx)

check_start=-1
ans = 1

if impossible:
    ans = 0
elif len(boxes[0])==0:
    ans = 0
elif boxes[0][0]!=0:
    ans = 0
elif len(boxes[0])>=2:
    ans = 0
else:
    for i in range(N-1):
        a = len(boxes[i])
        b = len(boxes[i+1])
        if a == 0:
            check_start = i
            rest_ls = boxes[i:]
            if rest_ls.count([])!=len(rest_ls):
                ans = 0
            break
        ans*=a**b
baisu = ans//998244353
print(ans-(998244353*baisu))

Solution (Python):
import math
N = int(input())
D = [int(x) for x in input().split()]

num = [0] * 100000

maxd = 0
for d in D:
    num[d] += 1
    maxd = max(d, maxd)

if D[0] != 0 or num[0] != 1:
    ans = 0
else:
    ans = 1
    for i in range(1, maxd):
        for _ in range(num[i+1]):
            ans *= num[i]%998244353
        ans %= 998244353
        if ans == 0:
            break
print(ans%998244353)

Solution (Python):
from collections import Counter


def pow(x,n):
    global p
    res=1
    prod=x
    while n>0:
        if n%2==1:
            res=(res*prod)%p
        prod=(prod*prod)%p
        n//=2
    return res

p=998244353

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

f=True
if D[0]!=0:
    f=False
else:
    C=Counter(D)
    if C[0]>1:
        f=False
    else:
        dmax=max(C)    
        for d in range(1,dmax):
            if d not in C:
                f=False
    if f:
        v=[C[i] for i in range(dmax+1)]
        ans=1
        for d in range(1,dmax):
            ans=(ans*pow(v[d],v[d+1]))%p
if f:
    print(ans)
else:
    print(0)

Solution (Python):
mod = 998244353
n = int(input())
d = list(map(int, input().split()))
ans = 1
l = [[i, 0] for i in range(n)]
for i in range(n): l[d[i]][1] += 1
while l[-1][1] == 0: l.pop(-1)
if d[0] != 0 or l[0][1] > 1: ans = 0
else:
    for i in range(len(l)-1):
        if l[i][1] > 0: 
            ans = (ans*(l[i][1]**l[i+1][1]))%mod
        else:
            ans = 0
            break
print(ans)

Solution (Python):
if __name__ == "__main__":
    mod = 998244353

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

    if d[0] > 0:
        print('0')
        exit()

    if len(d) == 1:
        print('1')
        exit()

    d.sort()
    d.pop(0)
    if d[0] == 0:
        print('0')
        exit()

    tmp = 1
    ans = 1
    max_num = max(d)
    dic = dict()
    for i in range(n-1):
        if d[i] in dic:
            dic[d[i]] += 1
        else:
            dic[d[i]] = 1

    for i in range(1, max_num+1):
        if i in dic:
            count = dic[i]
        else:
            print('0')
            exit()
        if count == 0:
            print('0')
            exit()
        ans *= tmp ** count
        ans %= mod
        tmp = count

    print(ans)

Solution (Python):
m = 998244353
def myPow(a,b):
    a %= m
    if b == 1:
        return a%m
    elif b == 0:
        return 1
    if b%2 == 0:
        return myPow(a**2,b//2)%m
    else:
        return (a%m)*myPow(a**2,(b-1)//2)%m

n = int(input())
ngo = [0]*n
d = list(map(int,input().split()))
for i in range(n):
    ngo[d[i]]+=1
if ngo[0] != 1:
    print(0)
    exit(0)
if d[0] != 0:
    print(0)
    exit(0)
ans = 1
for i in range(n-1):
    ans = (ans*myPow(ngo[i],ngo[i+1])%m)%m
print(ans%m)

Solution (Python):
n = int(input())
d = list(map(int , input().split()))
max_d = 0
cnt = [0] * 10**5
for i in d:
    cnt[i] += 1
    max_d = max(i, max_d)
if cnt[0] != 1 or d[0] != 0:
    ans = 0
else:
    ans = 1
    for i in range(1, max_d + 1):
        if cnt[i] == 0:
            ans = 0
            break
        ans *= cnt[i-1] ** cnt[i]
print(ans % 998244353)

Solution (Python):
import collections
N=int(input())
D_list=[int(thing) for thing in input().split(" ")]
maxx=max(D_list)
if D_list[0] !=0:
    print(0)
    exit()
C=collections.Counter(D_list)
if C[0] != 1:
    print(0)
    exit()

ret=1
for i in range(0,maxx):
    if maxx-i not in C:
        print(0)
        exit()
    if maxx-i==0:
        pass
    else:
        ret=ret*C[maxx-i-1]**C[maxx-i]

print(ret%998244353)

Solution (Python):

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

if D[0] != 0:
    flg = False
    
D.sort()
T = [0 for _ in range(N + 1)]

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

i = 0
ans = 1
if T[i] != 1:
    flg = False

pre = T[i]
i += 1
while T[i] != 0:
    ans *= pow(pre,T[i])
    ans %= mod
    pre = T[i]
    i += 1

while i < N:
    if T[i] > 0:
        flg = False
    i += 1

ans %= mod
if flg:
    print(ans)
else:
    print(0)

Solution (Python):
from collections import defaultdict

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

if D[0] != 0:
    print(0)
else:
    count = defaultdict(int)
    for d in D:
        count[d] += 1
    if count[0] != 1:
        print(0)
    else:
        maxcount = max(count)
        S = 1
        for i in range(1, maxcount+1):
            S *= count[i-1]**count[i]
        M = int(S % 998244353)
        print(M)

Solution (Python):

import sys


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

def input():
    return sys.stdin.readline().rstrip('\n')

def oint():
    return int(input())

N = oint()

d = list(rint())

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

d.sort()

if N > 1 and d[1] == 0:
    print(0)
    exit()

n = 1
c = 0
cprev = 1
ans = 1
for i in range(1, N):
    if d[i] == n:
        c+=1
    elif d[i] != n+1:
        print(0)
        exit()
    else:
        ans = (ans* pow(cprev, c, 998244353))%998244353
        n += 1
        cprev = c
        c = 1

ans = (ans* pow(cprev, c, 998244353))%998244353
print(ans)

Solution (Python):
import collections
import sys
def read_a_integer():
    return int(sys.stdin.readline())

def read_a_list(dtype):
    return list(map(dtype, sys.stdin.readline().split()))


def main():
    N = read_a_integer()
    D = read_a_list(int)
    if D[0] != 0:
        print(0)
        return
    counter = collections.Counter(D)
    if counter[0] != 1:
        print(0)
        return
    max_D = max(D)
    ans = 1
    for i in range(2, max_D + 1):
        if i not in counter:
            print(0)
            return
        ans = (ans * (counter[i-1] ** counter[i])) % 998244353
    print(ans)
    

        


if __name__ == "__main__":
    main()

Solution (Python):
def modpow(a, n, mod):
  res = 1
  while (n > 0):
    if (n & 1):
      res = res * a % mod 
    a = a * a % mod 
    n >>= 1

  return res

n = int(input())
d = list(map(int, input().split()))
D = {i:[] for i in range(max(d)+1)}
if sum(d) == 0 and n != 1:
  print(0)
  exit()
for i in range(n):
  if i == 0 and d[i] != 0:
    print(0)
    exit()
  if i != 0 and d[i] == 0:
    print(0)
    exit()
  D[d[i]] += [i]
ans = 1

for i in range(max(d)):
  a = modpow(len(D[i]), len(D[i+1]), 998244353)
  ans = ans * a % 998244353

print(ans % 998244353)

Solution (Python):
import collections
N = int(input())
D = list(map(int,input().split()))
longest = max(D)
c = collections.Counter(D)
ans = 1
if c[0] > 1 or D[0] != 0:
    print(0)
else:
    for i in range(longest):
        if c[0] > 1:
            ans = 0
            break
        ans = ans * (c[i])**(c[i+1]) % 998244353
    print(ans)

Solution (Python):
from sys import exit

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

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

c = {}
for i in D:
    if i in c:
        c[i] += 1
    else:
        c[i] = 1

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

result = 1
for i in range(1, max(D) + 1):
    if i not in c:
        print(0)
        exit()
    result = result * (c[i - 1] ** c[i]) % 998244353
print(result)

Solution (Python):
def answer():
    N = int(input())
    distances = list(map(int, input().split()))

    result = 1
    mod = 998244353

    if distances[0] != 0:
        result = 0
    elif 0 in distances[1:]:
        result = 0
    else:
        distances.sort()
        maximum = distances[-1]
        counts = [0] * (maximum + 1)
        counts[0] = 1
        for d in distances[1:]:
            counts[d] += 1
            result *= counts[d-1]
            result %= mod

    print(result)


if __name__ == '__main__':
    answer()

Solution (Python):
def answer():
    N = int(input())
    distances = list(map(int, input().split()))

    result = 1
    mod = 998244353

    if distances[0] != 0:
        result = 0
    elif 0 in distances[1:]:
        result = 0
    else:
        distances.sort()
        maximum = distances[-1]
        counts = [0] * (maximum + 1)
        counts[0] = 1
        for d in distances[1:]:
            counts[d] += 1
            result *= counts[d-1]
            result %= mod

    print(result)


if __name__ == '__main__':
    answer()

Solution (Python):
MOD = 998244353

n = int(input())
distances = list(map(lambda x: int(x), input().split()))


canSolve = True
ans = 1

if distances[0] != 0:
    canSolve = False
    ans = 0

dist_list = {}
if canSolve:
    for i in distances:
        if i in dist_list.keys():
            dist_list[i] += 1
        else:
            dist_list[i] = 1
            
            
    keys = list(dist_list.keys())
    keys.sort()
    keys.reverse()
    bigger = None
    smaller = None
    pre_v = None
    for v in keys:
        if bigger is None:
            bigger = dist_list[v]
            pre_v = v
            continue
        elif smaller is None:
            smaller = dist_list[v]
        else:
            bigger = smaller
            smaller = dist_list[v]
        
        if v==0 and smaller > 1:
            ans = 0

        if v == pre_v - 1:
            ans *= (smaller**bigger)
            ans = ans % MOD
            pre_v = v
        else:
            ans = 0
        
print(ans)

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

mod=998244353
c=collections.Counter(D)

if D[0]!=0 or c[0]!=1:
  print(0)
else:
  ans=c[0]
  node=c[0]
  for i in range(1,n):
    ans*=(c[i-1]**c[i])%mod
    node+=c[i]
    if node==n:
      break
  print(ans%mod)

Solution (Python):
import sys

pre_strk = 1
now_strk = 1
ans = 1
n = int(input())
d_list = list(map(int,input().split()))

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

d_list.sort()
if d_list[1] == 0:
    print(0)
    sys.exit()

for i in range(1,n):
    if d_list[i] == d_list[i-1]:
        now_strk += 1

    elif d_list[i] > d_list[i-1] + 1:
        print(0)
        sys.exit()

    elif d_list[i] != d_list[i-1] and d_list[i-1] != 1:
        for j in range(now_strk):
            ans *= pre_strk
        pre_strk = now_strk
        now_strk = 1
        
        ans = ans % 998244353

    else:
        pre_strk = now_strk
        now_strk = 1
for i in range(now_strk):
    ans *= pre_strk
print(ans%998244353)

Solution (Python):
n = int(input())
ls = list(map(int, input().split(' ')))
m = max(set(ls))
d = {}
for l in ls:
    d[l] = d.get(l, 0) + 1
ans = 0 if (ls[0] != 0 or d.get(0) >= 2) else 1
for i in range(1, m+1):
    if i not in d:
        ans = 0
    ans = (ans * (d.get(i-1, 0) ** d.get(i, 0))) % 998244353 
print(ans)

Solution (Python):
import sys
from collections import Counter

N = int(input())
D = [int(x) for x in input().split()]
D_counter = Counter(D)
ans = 1

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

for d in D_counter:
    if d - 1 in D_counter:
        ans *= (D_counter[d - 1] ** D_counter[d]) % 998244353
        ans = ans % 998244353
    else:
        if not d == 0:
            print(0)
            sys.exit()
print(ans)

Solution (Python):
import sys

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

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

d.sort()

if d[1] == 0:
    print(0)
    sys.exit()
    
dd = []
c = 1
for i in range(1, n):
    if d[i-1]==d[i]:
        c += 1
    else:
        dd.append(c)
        c = 1
dd.append(c)

if len(dd) != d[-1]+1:
    print(0)
    sys.exit()

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

ans = ans % 998244353
print(ans)

Solution (Python):
import sys
from collections import defaultdict, Counter, deque
from itertools import accumulate, permutations, combinations
from operator import itemgetter
from bisect import bisect_left, bisect_right, bisect
from heapq import heappop, heappush
from fractions import gcd
from math import ceil, floor, sqrt, cos, sin, pi
from copy import deepcopy




def solve(N,D):
    if D[0]:
        return 0

    try:
        _ = D[1::].index(0)
        return 0
    except:
        pass
    
    nums = Counter(D)
    nums = sorted(nums.items())
    if len(nums) != nums[-1][0]+1:
        return 0
        
    
    ans = 1
    for i in range(len(nums)-1):
        ans *= nums[i][1]**nums[i+1][1]
        ans %= 998244353
    return ans

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

if __name__ == '__main__':
    main()

Solution (Python):
import math
n=int(input())
dl=list(map(int,input().split()))
MOD=998244353
if dl[0]>0:
    print(0)
    exit()
dist=[0]*(n)
for d in dl:
    dist[d]+=1
if dist[0]!=1:
    print(0)
    exit()
ans=1
flg=False
for i in range(1,n):
    if dist[i]>0:
        if flg:
            ans=0
            break
        else:
            ans*=dist[i-1]**dist[i]%MOD
    else:
        flg=True
print(ans%MOD)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


@mt
def slv(N, D):
    if D[0] != 0:
        return 0
    C = Counter(D)
    if C[0] != 1:
        return 0
    ans = 1
    M = Mod(998244353)
    for i in range(max(D)):
        if C[i] == 0:
            return 0
        ans = M.mul(ans, M.pow(C[i], C[i+1]))
    return ans


def main():
    N = read_int()
    D = read_int_n()
    print(slv(N, D))


if __name__ == '__main__':
    main()

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 998244353

def main():
    N = int(input())
    D = [int(c) for c in input().split()]

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

    for i in range(1, N):
        if D[i] >= N or D[i] == 0:
            print(0)
            sys.exit()

    dist_nums = {}
    for i in range(N):
        if dist_nums.get(D[i]) is None:
            dist_nums[D[i]] = 1
        else:
            dist_nums[D[i]] += 1

    ans = 1
    num = 1
    dist_nums = sorted(dist_nums.items(), key=lambda x: x[0]) 
    for i in range(1, len(dist_nums)):
        if dist_nums[i][0] == i:
            ans *= dist_nums[i - 1][1] ** dist_nums[i][1]
            num += dist_nums[i][1]
        else:
            break

    if num != N:
        print(0)
    else:
        print(ans % MOD)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
D = input().split()
D = [int(d) for d in D]

hantei = 1
if(D[0] != 0):
    print(0)
else:
    D = sorted(D[1:])
    if(D[-1]==1):
        print(1)
    else:
        n = 0
        k = 0
        mul = 1
        k_count_before = 1
        while(n<len(D)):
            k += 1
            k_count = 0
            
            while(n<len(D) and D[n]==k):
                k_count += 1
                n += 1

            if(k_count==0):
                mul = 0
                hantei = 0
                break
            else:

                mul *= k_count_before**k_count
                k_count_before = k_count

        print(mul%998244353)

Solution (Python):
n=int(input())
l=list(map(int,input().split()))
l1=sorted(l)
m=l1[-1]
if l[0]!=0:
  print(0)
else:
  ld=[0 for i in range(m+1)]
  b=False
  for i in range(n):
    ld[l[i]]+=1
  for i in range(1+m):
    if ld[i]==0:
      b=True
  if ld[0]!=1:
    print(0)
  elif b:
    print(0)
  else:
    ans=1
    for i in range(1,m+1):
      num=ld[i-1]**ld[i]
      ans=(ans*num)%(998244353)
    print(ans%(998244353))

Solution (Python):
import collections

N = int(input())
D = list(map(int,input().split()))
D_counter = collections.Counter(D)
D_max = max(D)
ans = 1
if D[0] > 0:
    ans = 0
else:
    if D_counter[0] > 1:
        ans = 0
    for n in range(1,D_max+1):
        if D_counter[n] > 0:
            for d in range(D_counter[n]):
                ans = ans * D_counter[n-1]
                ans = ans % 998244353
        else:
            ans = 0
            break
print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10000000)
MOD = 998244353

def main():
    N = int(input())
    D = [int(c) for c in input().split()]

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

    for i in range(1, N):
        if D[i] >= N or D[i] == 0:
            print(0)
            sys.exit()

    dist_nums = {}
    for i in range(N):
        if dist_nums.get(D[i]) is None:
            dist_nums[D[i]] = 1
        else:
            dist_nums[D[i]] += 1

    ans = 1
    num = 1
    dist_nums = sorted(dist_nums.items(), key=lambda x: x[0]) 
    for i in range(1, len(dist_nums)):
        if dist_nums[i][0] == i:
            ans *= dist_nums[i - 1][1] ** dist_nums[i][1]
            num += dist_nums[i][1]
        else:
            break

    if num != N:
        print(0)
    else:
        print(ans % MOD)


if __name__ == '__main__':
    main()

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

ans = 1
if d[0] != 0 or sum(d) < n - 2 or sum(d) > (n * (n - 1) // 2) + 1:
    print(0)
else:
    l = [0] * (max(d) + 1)
    for i in range(n):
        l[d[i]] += 1

    if l[0] != 1:
        print(0)
    else:
        for j in range(1, len(l)):
            if l[j] == 0:
                ans = 0
                break
            else:
                x = l[j - 1] ** l[j] % mod
                ans *= x
                ans %= mod
        print(ans)

Solution (Python):
from collections import Counter

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


def pow_by_iter(a, x, n):
    acc = a
    for i in range(1, x):
        acc = (acc * a) % n
    return acc


d = Counter(D)
d_key = sorted(d.keys())
if D[0] != 0 or d[0] > 1:
    print(0)
    exit()


answer = 1

for i in range(1, len(d_key)):
    if d_key[i] != d_key[i - 1] + 1:
        print(0)
        exit()
    else:
        answer *= pow_by_iter(d[d_key[i - 1]], d[d_key[i]], mod)
        answer %= mod

print(answer)

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

Ans = 0
number = 1
n_count = 0
b_count = 1

newD = sorted(D)
if N == 1:
  Ans = 1
  print(Ans)
  sys.exit()

if D[0] != 0 or newD[1] != 1:
  print(Ans)
  sys.exit()
else:
  
  for i in range(1,N):
    if newD[i] == number:
      n_count = n_count + 1
    elif newD[i] == number + 1:

      if number == 1:
        Ans = 1
        
      else:
        Ans = Ans * (b_count ** n_count)
        if Ans > 998244353:
          Ans = Ans % 998244353
        
        
      number = number + 1
      b_count = n_count
      n_count = 1
    else:
      Ans = 0
      print(Ans)
      sys.exit()

  if number == 1:
    Ans = 1
  else:
    Ans = Ans * (b_count ** n_count)
    Ans = Ans % 998244353
  print(Ans)

Solution (Python):

import collections

N = int(input())
D = list(map(int, input().split()))
is_first_zero = True
if D[0] != 0:
    is_first_zero = False
if is_first_zero:
    c = sorted(collections.Counter(D).items(), key=lambda x:x[0])
    if len(c) == 2:
        res = 1
    for index, i in enumerate(c):
        ind = i[0]
        num = i[1]
        if ind == 0:
            if num != 1:
                is_first_zero = False
                break
        else:
            if ind != pre_ind + 1:
                is_first_zero = False
                break
            else:
                if index == 2:
                    res = pre_num ** num
                elif index > 2:
                    res = res * (pre_num ** num)
                
        pre_ind = ind
        pre_num = num
                
if is_first_zero:
    print(res % 998244353)
else:
    print("0")

Solution (Python):
import sys
import math
import bisect
from collections import defaultdict, deque
from heapq import heappush, heappop


def LI(): return [int(x) for x in sys.stdin.readline().split()]


def I(): return int(sys.stdin.readline())


def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res


def IR(n):
    return [I() for i in range(n)]



def A():
    n = I()
    print(n // 2 - 1 if n % 2 == 0 else n // 2)
    return



def B():
    n = I()
    d = LI()
    if d[0] != 0:
        print(0)
        return
    d.sort()
    if d[1] == 0:
        print(0)
        return
    cnt = []
    prenum = 1
    for i in range(1, max(d) + 1):
        num = bisect.bisect_right(d, i)
        if num - prenum == 0:
            print(0)
            return
        else:
            cnt.append(num - prenum)
            prenum = num
    ans = 1
    for i in range(1, max(d)):
        ans *= cnt[i - 1] ** cnt[i]
    print(ans % 998244353)
    return



def C():
    return



def D():
    return



def E():
    return



def F():
    return



def resolve():
    B()
    return



if __name__ == "__main__":
    B()

Solution (Python):


import sys
from collections import Counter

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for k in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for k in range(c)] for k in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 998244353

N = INT()
A = LIST()

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

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

depth = 0
ans = 1
prev = 1
for k, v in sorted(C.items()):
    if k != depth:
        print(0)
        exit()
    ans *= pow(prev, v, MOD)
    ans %= MOD
    depth += 1
    prev = v
print(ans)

Solution (Python):
N = int(input())
D = [int(s) for s in input().split()]

maximum = max(D)
max_distance = [i+1 for i in range(maximum)]
count_list = [0 for i in range(maximum+1)]
for i in D:
  count_list[i] += 1
if D[0] != 0 or count_list[0] != 1:
  print(0)
  exit()

stock = 1
for i in max_distance:
  count_1 = count_list[i-1]
  count_2 = count_list[i]
  if count_1 == 0 or count_2 == 0:
    stock = 0
    break
  stock *= count_1**count_2

print(stock%998244353)

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

if d[0]!=0:
	print(0)
else:
	d.sort()
	kotae=1
	kosulist=[]
	kazoe=1
	for i in range(1,n):
		if d[i]==d[i-1]:
			kazoe+=1
		else:
			kosulist.append(kazoe)
			kazoe=1
	kosulist.append(kazoe)
	
	kosukai=[1]
	for i in range(len(kosulist)-1):
		kosukai.append(kosukai[i]+kosulist[i+1])
	for i,a in enumerate(kosukai):
		if d[a-1]!=i:
			error=1
			break
			
	if error==0:		
		for i in range(len(kosulist)-1):
			kotae=kotae*((kosulist[i]**kosulist[i+1])%998244353)
		kotae=kotae%998244353
		print(int(kotae))
	else:
		print(0)

Solution (Python):
from collections import Counter

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

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

if D[0] == 0 and cntD[0] == 1 and all(cntD[i] >= 1 for i in range(R + 1)):
    print(ans)
else:
    print(0)

Solution (Python):
N=int(input())
ls=list(map(int,input().split()))
if ls[0]!=0:
  print(0)
  exit()
sls=sorted(ls)
if sls[1]==0:
  print(0)
  exit()
i=1
a=1
b=1
c=1
while i<N-1:
  if sls[i+1]==sls[i]:
    c+=1
  elif sls[i+1]==sls[i]+1:
    a=a*(b**c)
    b=c
    c=1
  else:
    a=0
    break
  i+=1
a=a*(b**c)
print(a%998244353)

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

C = [[] for _ in range(N)]
X = []
count = 0
ans = 1

for n in range(N):
    C[D[n]].append(D[n])

for i in range(1, max(D)):
    if len(C[i]) == 0:
        count += 1
        break

if count > 0:
    pass
elif D[0] != 0:
    count += 1
elif len(C[0]) != 1:
    count += 1
else:
    for j in range(1,max(D)+1):
        t = len(C[j-1]) ** len(C[j])
        X.append(t)
    for x in X:
        ans = x * ans
        ans = ans % 998244353
        
if count > 0:
    print(0)
else:
    print(int(ans))

Solution (Python):
n, *a = map(int, open(0).read().split())
if a[0]!=0:
    print(0)
    exit()

b = [0]*n
for i in a: b[i]+=1

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

ans = 1
i = 0
while i<n-1:
    if b[i]==0:
        if sum(b[i:])>0:
            print(0)
            exit()
        else:
            break
    ans *= b[i]**b[i+1]
    i += 1

print(ans%998244353)

Solution (Python):
from collections import Counter

N = int(input())
D = list(map(int, input().split()))
if D[0] > 0:
    print(0)
    exit(0)
cs = Counter(D)
if cs[0] > 1:
    print(0)
    exit(0)
count = 0
ans = 1
prev = 1
for i in range(N + 1):
    n = cs[i]
    if n == 0:
        if count == N:
            print(ans)
        else:
            print(0)
        exit(0)
    else:
        count += n
        ans = (ans * (prev ** n)) % 998244353
        prev = n

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

X = 998244353

l_max = max(D)

def f():
    if D[0] != 0:
        print(0)
    
        return
    L = [0] * (l_max+1)
    for d in D:
        L[d] += 1 
    if L[0] != 1:
        print(0)
        return
    ans = 1
    L = [1] + L
    

    for i in range(1, len(L)):
        if L[i] == 0:
            print(0)
            return
        ans = ans * (L[i-1] ** L[i])
    print(ans % X)
    
f()