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

mod = 998244353

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

d.sort()
ma = d[-1]

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

ans = 1
pre = 1
for i in range(ma+1):
    if c[i] == 0:
        print(0)
        exit()
    ans *= pow(pre, c[i], mod)
    ans %= mod
    pre = c[i]

print(ans)

Solution (Python):
N = int(input())
D = [int(i) for i in input().split()]
count = [0]*N
max_n = 0

for elem in D:
    count[elem] += 1
    if elem > max_n:
        max_n=elem

ans=1
if count[0] == 1 and D[0] == 0:
    for i in range(1,max_n+1):
        ans = (ans*((count[i-1])**count[i]))%998244353
else:
    ans = 0

print(ans%998244353)

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

a = l()

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

a.sort()

a = s(a)
count = 1

for i in range(1,len(a)):
    if i == 1 and a[i-1][1] != 1:
        print(0)
        exit()
    if a[i][0]-1 != a[i-1][0]:
        print(0)
        exit()
    count *= (pow(a[i-1][1],a[i][1]))%998244353  
    count %= 998244353
print(count)

Solution (Python):
n=int(input())
L=[int(i) for i in input().split()]
A=[0 for i in range(n)]
mod=998244353

for num in L:
    
    A[num]+=1

if A[0]!=1 or L[0]!=0:
    print(0)
else:
    ans=1
    for i in range(n-1):
        ans*=A[i]**A[i+1]
        ans=ans%mod
    print(ans%mod)

Solution (Python):
import sys
n=int(input())
d=list(map(int,input().split()))
if d[0]!=0:
  print(0)
  sys.exit()
d.sort()
for i in range(n-1):
  if d[i+1]-d[i]>=2:
    print(0)
    sys.exit()
x=1
ct=0
ctb=1
ans=1
mod=998244353
for i in range(1,n):
  if d[i]==x:
    ct+=1
  else:
    if x!=1:
      for j in range(ct):
        ans*=ctb
        ans%=mod
    ctb=ct
    ct=1
    x+=1
for i in range(ct):
  ans*=ctb
  ans%=mod
print(ans%mod)

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

mod = 998244353

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

from collections import Counter

counter = Counter(d_list)

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

prev = 0
max_d = max(d_list)
ans = 1
for i in range(1, max_d + 1):
    if i == 1:
        if counter[i] != 0:
            ans = 1
        else:
            print(0)
            exit()
    else:
        if counter[i] != 0:
            ans *= prev ** counter[i]
            ans %= mod
        else:
            print(0)
            exit()
    prev = counter[i]

print(ans)

Solution (Python):

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

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



ans = 1
inf = 998244353

for i in range(len(count)):
    if i == 0:
        pass
    else:
        tmp = pow(count[i-1], count[i], inf)
        ans *= tmp
        ans %= inf

if d[0] != 0:
    ans = 0
if count[0] != 1:
    ans = 0

print(ans)

Solution (Python):
from collections import defaultdict
N = int(input())
D = list(map(int, input().split()))
MOD = 998244353


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

if D.count(0) != 1:
    print(0)
    exit()


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


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

    ans *= X[d - 1]
    ans %= MOD

print(ans)

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

cnt = [0]*100100
for d in D:
    cnt[d] += 1
ans = int(D[0]==0 and cnt[0]==1)

check = 1
for i,c in enumerate(cnt[1:],1):
    if c == 0: break
    c0 = cnt[i-1]
    check += c
    ans *= pow(c0,c,mod)
    ans %= mod
if check != N:
    ans = 0
print(ans)

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

def pow(x, y):
    m = x
    ans = 1
    while y != 0:
        if y % 2 == 1:
            ans = (ans * m) % mod
        m = (m*m)% mod
        y //= 2
    return ans


import sys
if A[0] != 0:
    print(0)
else:
    B = [0] * (max(A)+1)
    for i in range(n):
        
        B[A[i]] += 1
    
    if B[0] != 1:
        print(0)
        sys.exit()
    for i in range(len(B)-1):
        ans = (ans * pow(B[i], B[i+1])) % mod
    print(ans)

Solution (Python):


def modpow(a,n,m):
    res = 1
    t = a
    while n:
        if n%2:
            res = (res*t)%m
        t = (t*t)%m
        n //= 2
    return res

MOD = 998244353

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

if D[0] != 0 or any(d == 0 for d in D[1:]):
    print(0)
    exit()

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



cnt = 1
for a,b in zip(memo, memo[1:]):
    cnt *= modpow(a,b,MOD)
    cnt %= MOD

print(cnt)

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

if d[0] != 0:
    ans = 0
else:
    d.sort()
    d.append(d[-1] + 1)
    if d[1] == 0:
        ans = 0
    else:
        x = 1
        cnt_prev = 1
        cnt = 0
        ans = 1
        for i in range(1, n + 1):
            if d[i] == x:
                cnt += 1
            elif d[i] == x + 1:
                
                ans *= cnt_prev**cnt
                ans %= MOD
                cnt_prev = cnt
                cnt = 1
                x += 1
            else:
                ans = 0
                break

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)
    exit()
ctr = Counter(D)
if ctr[0] != 1:
    print(0)
    exit()

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

Solution (Python):
from heapq import heappush, heappop
from collections import deque
import re
import math
import functools

def sRaw():
    return input().rstrip("\r")

def iRaw():
    return int(input())


def ssRaw():
    return input().split()


def isRaw():
    return list(map(int, ssRaw()))

INF = 1 << 29


def make1d_arr(n, val=INF):
    return [val for i in range(n)]


def make2d_arr(h, w, val=INF):
    return [[val for i in range(w)]for i in range(h)]


def gcd(a, b):
    if(b == 0):
        return a
    return gcd(b, a % b)


def BFord(n,es):
    v_cost = [INF]*n
    v_cost[0] = 0
    for v in range(n*2):
        for e in es:
            frm = e[0]
            to = e[1]
            if v_cost[to] > v_cost[frm]+e[2]:
                v_cost[to] = v_cost[frm]+e[2]
                if v >= n:
                    v_cost[to] = -INF
        if v == n-1:
            prev = v_cost[:]
    v_cost = [-INF if prev[idx]!=v_cost[idx] else v_cost[idx] for idx in range(n)]
    return v_cost
    
def ZAlgo(s):
    l = len(s)
    A = [0]*l
    i =1
    j =0
    A[0]=l
    while i<l:
        while i+j<l and s[j] == s[i+j]:
            j+=1
        if j==0:
            i+=1
            continue
        A[i]=j
        k=1
        while (i+k<l) and (k+A[k]<j):
            A[i+k] =A[k]
            k+=1
        i+=k
        j-=k
    return A


DIV = 998244353

def main():
    N = iRaw()
    Ds = isRaw()
    if Ds[0]!=0:
        return 0
    has_n = [0]*(N+1)
    has_n[0] = 1
    Ds = Ds[1:]
    Ds.sort()
    ans = 1
    for d in Ds:
        if d == 0:
            return 0
        else:
            if has_n[d-1]==0:
                return 0
            else:
                has_n[d]+=1
                ans = (ans*has_n[d-1])%DIV
    return ans


if __name__ == "__main__":
    print(main())

Solution (Python):
from operator import itemgetter
from itertools import groupby
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
if D[0]!=0:
    print(0)
    exit()
ID = sorted(enumerate(D[1:]), key=itemgetter(1))
ans = 1
pn = 1

for i, (v, g) in enumerate(groupby(ID, key=itemgetter(1)), 1):
    if i != v:
        ans = 0
    n = len(list(g))
    ans = (ans * pow(pn, n, mod)) % mod
    pn = n

print(ans)

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

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

ans=1
from collections import Counter
c=Counter(d)

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

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")

def main():
    md = 998244353
    n = int(input())
    dd = list(map(int, input().split()))
    if dd[0] > 0:
        print(0)
        exit()
    cnt = [0] * n
    for d in dd[1:]:
        if d == 0:
            print(0)
            exit()
        cnt[d] += 1
    ans = 1
    pc = 1
    for c in cnt[1:]:
        ans *= pow(pc, c, md)
        ans %= md
        pc = c
    print(ans)

main()

Solution (Python):
from collections import Counter
MOD = 998244353
n = int(input())
d = list(map(int, input().split()))
if d[0] != 0 or 0 in d[1:]:
    print(0)
    exit(0)
c = [0] * n
for x in d:
    c[x] += 1
t = 1
ans = 1
for i in range(n - 1):
    ans *= pow(t, c[i], MOD)
    t = c[i]
    ans %= MOD
print(ans)

Solution (Python):
from collections import defaultdict
mod = 998244353

n = int(input())
ds = list(map(int, input().split()))
if ds[0] != 0:
    print(0)
    exit(0)
ds = ds[1:]
dd = defaultdict(int)
dmax = -1
for d in ds:
    if d <= 0:
        print(0)
        exit(0)
    dd[d] += 1
    dmax = max(d, dmax)

for d in range(1, dmax+1):
    if d not in dd:
        print(0)
        exit(0)

total = 1
dd[0] = 1
for d in range(1, dmax+1):
    total *= (dd[d-1] ** dd[d])
    total = total % mod
print(total)

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

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 998244353

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[]]*num
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
D = III()

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

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

if count[0]>=2:
    print(0)
    exit()

for i in range(N+1)[::-1]:
    if count[-1]==0:
        count.pop()
    else:
        break

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

print(ans)

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

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

dist2 = sorted(dist[1:])
dist2.append(-1)
idx = 0
ans = 1
before = 1

for now in range(1, n):
	count = 0
	if dist2[idx] == -1:
		break
	if dist2[idx] != now:
		print(0)
		exit()
	while dist2[idx] == now:
		idx += 1
		count += 1

	ans *= (before ** count)
	ans %= mod
	before = count

print(ans)

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=998244353
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n=int(input())
    C=[0]*100001
    D=list(map(int,input().split()))
    if(D[0]!=0):
        print(0)
        return
    for d in D:
        C[d]+=1
    if(C[0]!=1):
        print(0)
        return
    for i in range(100000):
        if(C[i]==0 and C[i+1]!=0):
            print(0)
            return
    i=C.index(0)
    C=C[:i]
    ans=1
    for i in range(1,len(C)):
        ans*=C[i-1]**C[i]
        ans%=MOD
    print(ans)
resolve()

Solution (Python):
def main():
    mod=998244353
    n=int(input())
    d = list(map(int,input().split())) 
    if d[0]!=0:
        print(0)
        return
    cnt=[0 for _ in range(n-1)]
    for i in range(n-1):
        cnt[d[i+1]-1]+=1
    for i in range(len(cnt)):
        if cnt[len(cnt)-1-i]!=0:
            maxd=len(cnt)-i
            break
    ans=1
    for i in range(maxd-1):
        x=cnt[i]
        for _ in range(cnt[i+1]-1):
            x=(x*cnt[i])%mod
        ans=(ans*x)%mod

    print(ans)    

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
D = [int(d) for d in input().split()]
length = dict()
maxD = max(D)
mod = 998244353
for i in range(maxD + 1):
    length[i] = 0
for d in D:
    length[d] += 1
if D[0] != 0 or length[0] > 1:
    print(0)
else:
    ans = 1
    for i in range(1, maxD + 1):
        if length[i] == 0:
            print(0)
            break
        else:
            ans *= (length[i-1] ** length[i]) % mod
            ans %= mod
    else:
        print(ans)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n=I()
  l=LI()

  if l[0]!=0 or l.count(0)!=1:
    return 0

  ans=1
  l.sort()
  a=[0]*100010
  a[0]=1
  for x in l[1:]:
    a[x]+=1
    ans*=a[x-1]
    ans%=998244353

  return ans


print(main())

Solution (Python):
import os
import sys
from collections import Counter

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 998244353

N = int(sys.stdin.readline())
D = list(map(int, sys.stdin.readline().split()))


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

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

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

Solution (Python):
mod = 998244353
n = int(input())
l = list(map(int, input().split()))
m = [0]*(max(l)+1)
for x in l:
    m[x] += 1
if l[0] != 0 or not all(l[1:]) or not all(m):
    print(0)
    exit()
t = 1
r = 1
for x in m:
    t = t * pow(r,x,mod) % mod
    r = x

print(t)

Solution (Python):
n = int(input())
d = list(map(int,input().split()))
mod = 998244353
new = [0 for _ in range(max(d)+1)]
for x in d:
  new[x] += 1
if d[0] != 0:
  print(0)
elif new[0] != 1:
  print(0)
else:
  ans = 1
  for i in range(1,max(d)+1):
    ans *= new[i-1] ** new[i]
    
    ans %= mod
  print(ans)

Solution (Python):
N=int(input())
D=[int(i) for i in input().split()]
ans=1
mod=998244353
M=max(D)+1
X=[0 for i in range(M)]
for i in range(N):
    X[D[i]]+=1
if X[0]!=1:
    print(0)
    exit()
if D[0]!=0:
    print(0)
    exit()
if N==1:
    print(1)
    exit()
if N==2:
    if D[0]==0 and D[1]==1:
        print(1)
    else:
        print(0)
    exit()

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

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

mod = 998244353
di = defaultdict(int)
btr = 0
if d[0] != 0:
    print(0)
    exit()
for i in range(n):
    di[d[i]] += 1

keylist = sorted(list(di.keys()))
if di[0] != 1:
    print(0)
    exit()
result = 1
for i in range(1, len(keylist)):
    if i != keylist[i]:
        print(0)
        exit()
    result = result * (di[i-1] ** di[i]) % mod

print(result)

Solution (Python):
from collections import Counter

N = int(input())
d = list(map(int, input().split()))
max_d = max(d)
c = Counter(d)
mod = 998244353

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

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

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

ans = 1
prev = 1
for i in range(1, max_d + 1):
    if c[i] == 0:
        ans = 0
    ans = (ans * pow(prev, c[i], mod)) % mod
    prev = c[i]

print(ans)

Solution (Python):

import sys
input = sys.stdin.buffer.readline
con = 998244353


from collections import defaultdict


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


def main():
	N = int(input())
	D = getlist()
	M = max(D)
	dic = defaultdict(int)
	for i in D:
		dic[i] += 1
	if D[0] != 0 or dic[0] >= 2:
		print(0)
	else:
		ans = 1
		for i in range(M):
			x = dic[i]
			y = dic[i + 1]
			for j in range(y):
				ans = ans * x
				ans %= con
		print(ans)
	
if __name__ == '__main__':
	main()

Solution (Python):
n=int(input())
dlist=[int(i) for i in input().split()]
if dlist[0]!=0:
    print(0)
else:
    dlist=sorted(dlist)
    before=0
    kyolist=[0]
    dame=0
    for i in range(n):
        if dlist[i]==before:
            kyolist[-1]+=1
        elif dlist[i]==before+1:
            kyolist.append(1)
        else:
            dame=1
        before=dlist[i]
    
    if dame==1 or kyolist[0]!=1:
        print(0)
    else:
        if len(kyolist)<=2:
            print(1)
        else:
            answer=1
            for i in range(len(kyolist)-1):
                answer*=(kyolist[i]**kyolist[i+1])
            print(answer%998244353)

Solution (Python):
MOD = 998244353


def main():
    N = int(input())
    D = list(map(int, input().split()))
    M = max(D)
    cnt = [0] * (M+1)
    for x in D:
        cnt[x] += 1
    if D[0] != 0 or cnt[0] != 1:
        print(0)
        return
    for x in range(1, M+1):
        if cnt[x] == 0:
            print(0)
            return
    ans = 1
    for i in range(1, M):
        ans = (ans * pow(cnt[i], cnt[i+1], MOD)) % MOD
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
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 LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 998244353

def solve():
    n = I()
    d = LI()
    f = defaultdict(lambda : 0)
    for i in d:
        f[i] += 1
    m = max(d)
    ans = 1
    if f[0] != 1:
        print(0)
        return
    if d[0] != 0:
        print(0)
        return
    for i in range(m):
        ans *= pow(f[i],f[i+1],mod)
        ans %= mod
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
mod = 998244353
N = int(input())
D = list(map(int, input().split()))
if D[0] !=0:
    print(0)
else:
    cnt = [0]*N
    for i, d in enumerate(D):
        cnt[d]+=1
    cum = 1
    ans = 1
    for i in range(1, N):
        c = cnt[i]
        if c==0:
            break
        ans*=pow(cnt[i-1], c, mod)
        ans%=mod
        cum+=c
    if cum == N:
        print(ans)
    else:
        print(0)

Solution (Python):
import sys
readline = sys.stdin.readline
mod = 998244353
N = int(readline())
D = list(map(int, readline().split()))
cnt = [0]*(N+1)
for d in D:
    cnt[d] += 1

ans = 0
if cnt[0] != 1:
    ans = 0
elif D[0] != 0:
    ans = 0
elif len(set(D)) != 1 + max(D):
    ans = 0
else:
    ans = 1
    for i in range(1, N+1):
        dp = cnt[i-1]
        dn = cnt[i]
        if dn == 0:
            break
        ans = (ans*pow(dp, dn, mod)) %mod
print(ans)

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

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

mod=998244353

from collections import Counter
C=Counter(D)

FACT=[1]
for i in range(1,2*10**5+1):
    FACT.append(FACT[-1]*i%mod)

FACT_INV=[pow(FACT[-1],mod-2,mod)]
for i in range(2*10**5,0,-1):
    FACT_INV.append(FACT_INV[-1]*i%mod)

FACT_INV.reverse()

def Combi(a,b):
    if 0<=b<=a:
        return FACT[a]*FACT_INV[b]*FACT_INV[a-b]%mod
    else:
        return 0


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

MAX=max(D)

for i in range(MAX):
    if C[i]==0:
        print(0)
        sys.exit()

ANS=1

for i in range(1,MAX+1):
    ANS=ANS*pow(C[i-1],C[i],mod)%mod

print(ANS)

Solution (Python):











def solve():
    from collections import defaultdict
    import sys
    input = sys.stdin.readline
    mod = 998244353

    n = int(input())
    d = list(map(int, input().split()))
    if d[0] != 0:
        return 0

    dic = defaultdict(int)
    for d_ in d[1:]:
        if d_ == 0:
            return 0
        dic[d_] += 1

    ans = 1
    pre_key = 0
    pre_value = 1
    for key, value in sorted(dic.items()):
        if key != pre_key + 1:
            return 0
        ans = ans * (pre_value ** value) % mod
        pre_key = key
        pre_value = value
    return ans


if __name__ == '__main__':
    print(solve())

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

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

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

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

ans = 1
for i in range(1,n):
    ans*=pow(a[i-1],a[i],m)

print(ans%m)

Solution (Python):

import sys
sys.setrecursionlimit(200000)
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def fi(): return float(input())
def mfi(): return map(float, input().rstrip().split())
def lmfi(): return list(map(float, input().rstrip().split()))
def li(): return list(input().rstrip())
def debug(*args, sep=" ", end="\n"): print("debug:", *args, file=sys.stderr, sep=sep, end=end) if not __debug__ else None
def exit(*arg): print(*arg); sys.exit()



def main():
    N = ii()
    a = lmi()
    if a[0] != 0:
        exit(0)

    a.sort()
    ma = max(a)
    if a[1] == 0:
        exit(0)
    ans = 1
    dp = [0] * N
    for x in a:
        dp[x] += 1
    for i in range(1, ma + 1):
        ans = ans * pow(dp[i - 1], dp[i], 998244353) % 998244353
    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    mod = 998244353
    N = int(input())
    D = list(map(int, input().split()))
    count = dict()
    for i in range(N):
        count[D[i]] = count.get(D[i], 0) + 1

    if D[0] != 0: return 0
    if 0 in D[1:]: return 0

    cnt = 1
    ans = 1
    prev = 1
    for i in range(1, N):
        if i in count:
            ans *= pow(prev, count[i]) % mod
            ans %= mod
            prev =  count[i]
            cnt += count[i]
            if cnt == N:
                return ans % mod
        else:
            return 0
    return 0


if __name__ == '__main__':
    
    print(main())

Solution (Python):

import sys
from collections import deque, defaultdict
import copy
import bisect
sys.setrecursionlimit(10 ** 9)
import math
import heapq
from itertools import product, permutations,combinations
import fractions
from operator import itemgetter


import sys
def input():
	return sys.stdin.readline().strip()

N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
	print(0)
	exit()


num_list = defaultdict(int)
max_num = max(D)
num_list[0] += 1

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

mod = 998244353
ans = 1
for i in range(max_num):
	ans *= pow(num_list[i], num_list[i + 1], mod)
	ans %= mod

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

import numpy as np
MOD = 998244353

N = int(readline())
D = np.array(read().split(),np.int32)

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

counter = np.bincount(D,minlength=N)

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

arr = counter[D[1:] - 1]

def cumprod(arr,MOD):
    L = len(arr); Lsq = int(L**.5+1)
    arr = np.resize(arr,Lsq**2).reshape(Lsq,Lsq)
    for n in range(1,Lsq):
        arr[:,n] *= arr[:,n-1]; arr[:,n] %= MOD
    for n in range(1,Lsq):
        arr[n] *= arr[n-1,-1]; arr[n] %= MOD
    return arr.ravel()[:L]

answer = cumprod(arr,MOD)[-1]
print(answer)

Solution (Python):
from collections import Counter as C
N = int(input())
A = [int(a) for a in input().split()]
B = C(A)

if A[0] != 0 or A.count(0) != 1:
    print(0)
else:
    ans = 1
    for i in range(1, max(A) + 1):
        ans = ans * pow(B[i-1], B[i], 998244353) % 998244353
    print(ans)

Solution (Python):
N = input()
D = map(int, raw_input().split())
if D[0]!=0 or N==1 or min(D[1:])==0:
  print 0
  exit(0)

D = D[1:]
D.sort()
C = [0]*N
C[0] = 1
ans = 1
for d in D:
  ans *= C[d-1]
  ans %= 998244353
  C[d] += 1
print ans

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))