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())
array = list(map(int, input().split()))
if array[0] != 0:
    print(0)
    exit()
array.sort()
if 1 in array and len(set(array[1:])) == 1:
    print(1)
    exit()
if not (2 in array):
    print(0)
    exit()
if 0 in array[1:]:
    print(0)
    exit()
index = array.index(2)
count = 1
count_old = array.count(1)
ans = 1
first = array[index]
for i in range(index,N-1):
    if first != array[i+1]:
        if first != array[i+1] - 1:
            print(0)
            exit()
        ans *= count_old ** count
        count_old = count
        count = 1
    else:
        count += 1
    first = array[i+1]
    ans %= 998244353
ans *= count_old ** count
ans %= 998244353
print(ans)

Solution (Python):
from sys import stdin
import bisect
from itertools import groupby
 
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
 
if li[0] != 0 or 0 in li[1:]:
    print(0)
    exit()

li.sort()
lin = []

for key, value in groupby(li):
    lin.append((key,len(list(value))))
 
point = 1
for i in range(1,len(lin)):
    if lin[i-1][0]+1 != lin[i][0]:
        print(0)
        exit()
    elif lin[i][1] == 0:
        print(0)
        exit()
    point *= (lin[i-1][1]**lin[i][1])
 
print(point%998244353)

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

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

cnt=[0]*(max(D)+1)
mod=998244353
for i in D:
    cnt[i]+=1
if cnt[0]!=1 or D[0]!=0:
    print(0)
    exit(0)
ans=1
for i in range(1,len(cnt)):
    if cnt[i]==0:
        print(0)
        exit()
    ans*=pow(cnt[i-1],cnt[i],mod)
    ans%=mod
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
count=1
x=max(a)
import collections
c=collections.Counter(a)
if a[0]!=0 or c[0]!=1:
  print(0)
  exit()
else:
  b=[1]*(x+1)
  for i in range(1,x+1):
    count=(count*(b[i-1]**c[i]))%998244353
    b[i]=c[i]
  print(count)

Solution (Python):
def main():
    MOD = 998244353
    N = int(input())
    D = [int(i) for i in input().split()]
    if D[0] != 0:
        return print(0)
    from collections import Counter
    c = Counter(D)
    ans = 1
    for d in D[1:]:
        if d == 0:
            return print(0)
        ans *= c[d-1]
        ans %= MOD
    print(ans % MOD)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
M = 998244353

n = int(input())
d = list(map(int, input().split()))
idx = [0]*n
ans = 1
for i in range(n):
    idx[d[i]] += 1

if not d[0] == 0 or not idx[0] == 1:
    print(0)
else:
    for i in range(1,n):
        if idx[i-1] == 0:
            if not idx[i] == 0:
                print(0)
                sys.exit()
        else:
            ans *= idx[i-1]**idx[i]%M
    else:
        print(ans%M)

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

Solution (Python):
from collections import Counter
 
M = 998244353
n = int(input())
a = map(int, input().split())
x, c, r = next(a), Counter(a), 0
if x == c[0] == 0:
    r = p = i = 1
    while c[i]:
        r = r * pow(p, c[i], M) % M
        p = c[i]; n -= p; i += 1
print((r, 0)[n > 1])

Solution (Python):
from collections import Counter
 
M = 998244353
n = int(input())
a = map(int, input().split())
x, c, r = next(a), Counter(a), 0
if x == c[0] == 0:
    r = p = i = 1
    while c[i]:
        r = r * pow(p, c[i], M) % M
        p = c[i]
        n -= p
        i += 1
print((r, 0)[n > 1])

Solution (Python):
from collections import Counter
 
M = 998244353
n = int(input())
a = map(int, input().split())
x, c = next(a), Counter(a)
r = 0
if x == c[0] == 0:
    r = p = i = 1
    while c[i]:
        r = r * pow(p, c[i], M) % M
        p = c[i]
        n -= p
        i += 1
print((r, 0)[n > 1])

Solution (Python):
import sys

n = int(input())
d_list = [num for num in map(int, input().split(' '))]




ans = 0

d_max = max(d_list)
layer = [0] * (d_max+1)
touri = [0] * (d_max+1)

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

for i in range(1, n):
  d = d_list[i]
  if d == 0:
    print(0)
    sys.exit()
  layer[d] += 1

touri[0] = 1
for i in range(1, len(layer)):
  count = layer[i]
  prev_count = layer[i-1]
  
  if prev_count == 1:
    touri[i] = 1
    continue
  
  if count == 1:
    touri[i] = prev_count
    continue
  
  touri[i] = prev_count ** count
    
ans = 1
for i in touri:
  ans = (ans * i) % 998244353
  


print(ans)

Solution (Python):
def f_b():
    n, *d = map(int, open(0).read().split())
    c = [0]*(max(d)+1)
    for i in range(n):
        c[d[i]] += 1

    a = 0 if d[0]!=0 or c[0]>1 else 1
    m = 998244353
    for i in range(1, len(c)):
        a = a*c[i-1]**c[i]%m

    print(a)

if __name__ == "__main__":
    f_b()

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

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

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

cnt = Counter(d)

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

ans = 1
for i in d:
    if i != 0:
        ans = (ans * cnt[i-1]) % 998244353
print(ans)

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 18
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 998244353


def main():
    m = I()
    D = LI()
    if D[0] != 0:
        return 0
    cnt = Counter(D)
    ret = 1
    for i in range(max(D) + 1):
        val = cnt[i]
        if not val:
            return 0
        else:
            if i == 0:
                if val != 1:
                    return 0
            elif i == 1:
                pre = val
            else:
                ret = pow(pre, val, mod) * ret % mod
                pre = val
    return ret


print(main())

Solution (Python):
from collections import Counter
 
M = 998244353
n = int(input())
a = map(int, input().split())
x, c = next(a), Counter(a)
r = 0
if x == c[0] == 0:
    r = p = i = 1
    while c[i]:
        r = r * pow(p, c[i], M) % M
        p = c[i]
        n -= p
        i += 1
print((r, 0)[n > 1])

Solution (Python):
import sys
from collections import defaultdict
INF = 998244353
N = int(input())
dl = list(map(int, input().split()))
if dl[0] != 0:
    print(0)
    sys.exit()
d = defaultdict(int)

for i in range(N):
    d[dl[i]] += 1

d = sorted(d.items(), key=lambda x: x[0])

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

ans = 1
for i in range(len(d) - 1, 0, -1):
    if d[i][0] - 1 == d[i - 1][0]:
        ans *= (d[i - 1][1] ** d[i][1])
        ans = ans % INF
    else:
        print(0)
        sys.exit()

print(ans)

Solution (Python):

import sys

MOD = 998244353  

def baai(n1, n2):
    return(n1**n2%MOD)

def solve(N: int, D: "List[int]"):
    cn=[0]*(N + 1)
    for i in range(N):
        cn[D[i]]+=1

    if D[0]!=0:
        print(0)
    else:
        answer=1
        bak=cn[0]
        kyo=1
        hen=1
        while True:
            cnt=cn[kyo]
            hen+=cnt
            if cnt==0:
                break
            answer=answer*baai(bak, cnt)%MOD
            bak=cnt
            kyo+=1
        if hen==N:
            print(answer%MOD)
        else:
            print(0)
    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
import collections
sys.setrecursionlimit(10**9)
def input():
    return sys.stdin.readline().rstrip()

def main():
    N=int(input())
    D=list(map(int,input().split()))
    c=collections.Counter(D)
    k=list(c.keys())
    v=list(c.values())
    ans=1 if c[0] == 1 and D[0] == 0 else 0
    for i in range(max(k)):
        ans*=c[i]**c[i+1]
        ans%=998244353
        if ans == 0:
            break
    print(ans)

if __name__ == '__main__':
    main()

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)
if c[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(1, len(c)):
    if c[i] == 0:
        print(0)
        exit()
    ans *= c[i - 1]**c[i]
    ans %= MOD
print(ans)

Solution (Python):
from itertools import groupby

M = 998244353
n = int(input())
a = map(int, input().split())
r = 0
if next(a) == 0:
    r = p = 1
    for i, (k, g) in enumerate(groupby(sorted(a)), 1):
        if k != i:
            r = 0
            break
        l = sum(1 for _ in g)
        r = r * pow(p, l, M) % M
        p = l
print(r)

Solution (Python):
import sys

a = 998244353
n = int(input())
D = list(map(int,input().split()))
if n==1 and D[0]==0:
    print(1)
    sys.exit()
elif D[0]!=0:
    print(0)
    sys.exit()
else:
    D.sort()
    if D[1]!=1:
        print(0)
        sys.exit()
    else:
        cnt = 1
        precnt = 1
        ans = 1
        for i in range(n-2):
            if D[i+2]-D[i+1]>=2:
                print(0)
                sys.exit()
            elif i+2 == n-1:
                if D[i+2]==D[i+1]:
                    cnt += 1
                    ans *= precnt**cnt
                else:
                    ans *= precnt**cnt
                    precnt = cnt
                    ans *= precnt
            elif D[i+2] == D[i+1]:
                cnt += 1
            else:
                ans *= precnt**cnt
                precnt = cnt
                cnt = 1
    print(ans%a)

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

import sys

MOD = 998244353  

def baai(n1, n2):
    return(n1**n2%MOD)

def solve(N: int, D: "List[int]"):
    cn=[0]*(N*2)
    for i in range(N):
        cn[D[i]]+=1

    if D[0]!=0:
        print(0)
    else:
        answer=1
        bak=cn[0]
        kyo=1
        hen=1
        while True:
            cnt=cn[kyo]
            hen+=cnt
            if cnt==0:
                break
            answer=answer*baai(bak, cnt)%MOD
            bak=cnt
            kyo+=1
        if hen==N:
            print(answer%MOD)
        else:
            print(0)
    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):

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

Solution (Python):
def MAP(): return list(map(int,input().split()))
def INT(): return int(input())
def FLOAT(): return float(input())
MOD = 998244353


n = INT()
d = MAP()

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

l = [0]*(max(d)+1)
for i in d:
    l[i]+=1

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

Solution (Python):
from collections import defaultdict
N = int(input())
d_list = [int(d) for d in input().split(' ')]


def search(d_list):
    if d_list[0] != 0:
        return 0

    d_dict = defaultdict(int)
    unique_values = set()
    for d in d_list:
        d_dict[d] += 1
        unique_values.add(d)

    if d_dict.get(0) and d_dict.get(0) >= 2:
        return 0

    unique_num = len(unique_values)
    if not sum(unique_values) == unique_num * (unique_num - 1) / 2:
        return 0

    result = 1

    for v in unique_values:
        if v == 0:
            continue
        prev_node_num = d_dict.get(v-1)
        current_node_num = d_dict.get(v)

        result *= prev_node_num ** current_node_num
    return result % 998244353

print(search(d_list))

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

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

for i in range(1,n):
    distance_dict[d[i]] += 1
pre = 1
ans = 1
for i in range(1,n):
    if i in distance_dict:
      ans *= pre ** distance_dict[i]
      pre = distance_dict[i]
      ans %= mod
      if i != 1 and i - 1 not in distance_dict:
          print(0)
          exit()
print(ans)

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

Solution (Python):
import collections
N=int(input())
d=list(map(int,input().split()))
dd = collections.Counter(d)
if dd[0]!=1 or d[0]!=0:
    print(0)
    exit()
count=1
for i in range(1,max(dd)+1):
    count *= pow(dd[i-1],dd[i],998244353)
print(count%998244353)

Solution (Python):
import collections

n = int(input())
li = list(map(int,input().split()))
 
ans = 1
ans_li = collections.Counter(li)

if not li[0] == 0:
    print(0)
    exit()

if not ans_li[0] == 1:
    print(0)
    exit()

for i in range(max(ans_li)+1):
    ans *= ans_li[i] ** ans_li[i+1]
    if ans_li[i] == 0:
        print(0)
        exit()
 
print(ans % 998244353)

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

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

ans = 1
setD = list(set(d))




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

        for _d in setD:
                if _d != 0:
                        ans *= c[_d - 1] ** c[_d]

        print(ans%998244353)

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

Solution (Python):
from collections import Counter
N = int(input())
Dlist = list(map(int, input().split()))
DCounter = Counter(Dlist)
if DCounter[0] == 1 and Dlist[0] == 0:
    num = 1
    for i in range(1, max(Dlist) + 1):
        num *= DCounter[i - 1] ** DCounter[i]

    print(num % 998244353)
else:
    print(0)

Solution (Python):
import collections

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

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

a.sort()

c = collections.Counter(a)

ans = 1
now = 1
if a[0] != 0 or a[1]==0:
    print(0)
else:
    last = a[-1]
    for i in range(1,last+1):
        cnt = c[i]
        if cnt==0:
            ans = 0
            break
        else:
            ans *= ((now**cnt)%998244353)
            ans %= 998244353
            now = cnt
    print(ans)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
ans=1
x=sorted(d)
acount=1
count=1
sd=set(x)
sd=list(sd)
s=0

for i in range(1,n-1):
  if x[i]==x[i+1]:
    acount+=1
  elif x[i]!=x[i+1]:
    ans*=(count**acount)
    count=acount
    acount=1
  
if len(x)>1 and x[-1]!=x[-2]:
  ans*=count
elif len(x)>1 and x[-1]==x[-2]:
  ans*=(count**acount)

  
for i in range(len(sd)-1):
  if sd[i]+1!=sd[i+1]:
    s=1
    
if s==0:
  if d[0]==0 and x[1]!=0:
    print(ans%998244353)
  else:
    print(0)
else:
  print(0)

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

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

MOD = 998244353
L = [0]*(n+1)

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


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

zerofirst = L.index(0)
for i in range(n-1,-1,-1) :
  if L[i] != 0 :
    nonzerolast = i
    break

if zerofirst < nonzerolast :
  print(0)
  exit()

ans = 1
for i in range(1,nonzerolast+1) :
  ans *= (L[i-1]) ** (L[i])
  ans %= MOD

print(ans)

Solution (Python):
import collections
mod = 998244353

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

if d[0]!=0:
    print(0)
else:
    d.sort()
    d_max = d[-1]
    a = 1
    count = 0
    l = collections.Counter(d)
    for i in range(d_max+1):
        if l[i]==0:
            count +=1
            break

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

    if count ==1:
        print(0)
    else:
        for i in range(1,d_max+1):
            a = a*(l[i-1]**l[i])
        print(a%mod)

Solution (Python):

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

import math

def func(i, j, d):
    if i <= 1:
        return 1
    e = j
    b = int(math.log2(d)/math.log2(i))
    ret = 1
    while e > b:
        ret = ret*(i**b%d)
        e = e - b
    else:
        if e > 0:
            ret = ret*(i**e%d)
    return ret


def main():
    N = int(input())
    Ds = [int(d) for d in input().split()]

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

    Ds.sort()
    if Ds[1] == 0:
        print(0)
        return

    i = 1
    l = 1
    r = 1
    ans = 1
    divider = 998244353

    while i < N:
        cnt = 0
        while i < N and Ds[i] == l:
            cnt = cnt + 1
            i = i + 1
        if cnt == 0:
            print(0)
            return

        ans = ans*func(r, cnt, divider)%divider
        r = cnt
        l = l + 1
    
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
from collections import Counter
D = [int(i) for i in input().split()]
c = Counter(D)


if D[0] != 0:
  print(0)
  quit()
if c[0] != 1:
  print(0)
  quit()
if len(c.keys()) != max(c.keys())+1:
  print(0)
  quit()

  
res = 1
for i in range(1,1+max(c.keys())):
  res *= c[i-1]**c[i]
  res %= 998244353
print(res)

Solution (Python):

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

c = collections.Counter(D)
ans = 1
tmp = 1
if D[0] > 0:
    print(0)
    exit()
elif c[0] != 1:
    print(0)
    exit()
else:
    for i in range(1,max(D)+1):
        ans *= (tmp ** c[i])
        ans = ans % 998244353
        tmp = c[i]
print(ans)

Solution (Python):
import sys
n=int(input())
a,b,c,d,i=list(map(int,input().split())),0,[],1,0
if a[0]!=0:
    print('0')
    sys.exit()
if n==1:
    print('1')
    sys.exit()
a.sort()
if a[1]==0:
    print('0')
    sys.exit()
while True:
    if a[i]==a[i+1]:
        b+=1
    else:
        c.append(b+1)
        b=0
    i+=1
    if i+1>=n:
        c.append(b+1)
        break
if len(c)-1!=max(a):
    print('0')
    sys.exit()
for i in range(len(c)-1):
    d*=c[i]**c[i+1]
print(d%998244353)

Solution (Python):
from collections import Counter

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

count = Counter(d)

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






elif count[0] != 1:
    print(0)
    
else:
    ans = 1
    tmp = 1
    max_d = max(d)
    for i in range(1, max_d+1):
        
        ans *= tmp**count[i]
        
        tmp = count[i]


    print(ans%998244353)

Solution (Python):
import sys
n=int(input())
d=list(map(int,input().split()))
num_d=[0 for i in range(n)]
num_d[0]=1
if d[0]!=0:
  print(0)
  sys.exit()
for i in range(1,len(d)):
  if d[i]==0:
    print(0)
    sys.exit()
  num_d[d[i]]+=1
max_d=0
ans=1
for i in range(1,n):
  if num_d[i]!=0:
    if max_d!=i-1:
      print(0)
      sys.exit()
    max_d=i
    ans*=num_d[i-1]**num_d[i]
    ans%=998244353
print(ans)

Solution (Python):
import collections
n = int(input())
a = [int(i) for i in input().split()]
b = collections.Counter(a)
branch = b[1]
ct = 1
for i in range(max(a)+1):
    if i == 0:
        continue
    else:
        ct *= b[i-1]**b[i]
if b[0] != 1 or a[0] != 0:
    print(0)
else:
    print(ct%998244353)

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

Solution (Python):
n = int(input())
d_array = [int(x) for x in input().split()]
counter = {x: 0 for x in range(-1, 10 ** 5 + 1)}
for i in d_array:
    counter[i] += 1
MOD = 998244353
if d_array[0] != 0 or counter[0] != 1:
    print(0)
    exit()
ans = 1
for num in set(d_array):
    if num == 0:
        continue
    if counter[num-1] == 0:
        print(0)
        exit()
    ans *= (counter[num-1])**counter[num]
    ans = ans % MOD
else:
    print(ans)

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

Solution (Python):
import sys

N = int(input())

lis = [0] * (10 ** 5 + 10)

D = list(map(int,input().split()))
maxi = 0

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

for i in D:
    maxi = max(maxi,i)
    lis[i] += 1

ans = 1
for i in range(maxi + 1):

    if i == 0 and lis[i] != 1:
        ans = 0
        print (0)
        sys.exit()
    elif i != 0:
        
        ans = ans * ( lis[i-1] ** lis[i] ) 
        ans %= 998244353


print (ans % 998244353)

Solution (Python):
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


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

    C = [0] * (max(D) + 1)
    for d in D:
        C[d] += 1
    
    
    if D[0] != 0 or C[0] >= 2:
        print(0)
        return
    
    ans = 1
    for i in range(len(C) -1):
        for _ in range(C[i+1]):
            ans *= C[i]
            ans %= 998244353
    
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

M = 998244353
n = int(input())
d = list(map(int, input().split()))
counter = Counter(d)

if d[0] != 0 or counter[0] != 1:
    ans = 0
else:
    ans = counter[0]
    for i in range(1, max(d) + 1):
        if counter[i] == 0:
            ans = 0
            break
        m, n = counter[i - 1], counter[i]
        c = m ** n
        ans *= c

print(ans % M)

Solution (Python):
def main():
    import collections
    import sys
    input = sys.stdin.readline
    n = int(input())
    d = list(map(int, input().split()))
    
    c = collections.Counter(d)
    ans = 1
    tmp = -1
    
    if d[0] != 0 or c[0] > 1:
        print(0)
        exit()
    
    for i in range(2, n):
        if i - 1 in c.keys():
            ans *= c[i - 1] ** c[i]
            ans %= 998244353
        else:
            tmp = i
            break
    
    if tmp >= 0:
        for j in range(tmp, n):
            if j in c.keys():
                print(0)
                exit()
    print(ans)


if __name__ == "__main__":
    main()

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 *= (tmp ** d[i] )%998244353
    tmp = d[i]
print(res%998244353)

Solution (Python):
from sys import stdin
from collections import Counter
N = int(stdin.readline().rstrip())
D = [int(x) for x in stdin.readline().rstrip().split()]
mod = 998244353
ans = 1
if N == 1 and D[0] == 0:
    print(1)
elif len(set(D))==1:
    print(0)
elif D.count(0) != 1:
    print(0)
elif D[0] != 0:
    print(0)
else:
    tmp = 1
    cnt = Counter(D)
    
    flag = True
    for i in range(1,max(D)+1):
        
        if i != 1 and cnt[i] != tmp:
            flag = False
        c = (tmp**cnt[i]) % mod
        ans *= c % mod
        tmp = cnt[i]
        ans = ans % mod
    if flag:
        print(1)
    else:
        print(ans % mod)

Solution (Python):
n = int(input())
L = [int(i) for i in input().split()]
LL = [0 for i in range(pow(10,5)+1)]
 
for i in range(n):
  LL[L[i]] += 1
  
MOD = 998244353
out = 1
maximum = max(L)
for i in range(maximum):
  out = (out * pow(LL[i], LL[i+1], MOD)) % MOD
 
if L[0] != 0 or LL[0] != 1:
  out = 0

for i in range(maximum):
  if L[i+1] == 0:
    out = 0
  
print(out)

Solution (Python):
import collections
import sys

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

cnt = collections.Counter(d)

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

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

for i in range(1,max(d)+1):
    p = (p * pow(cnt[i-1], cnt[i], 998244353))%998244353

print(p)

Solution (Python):
import collections
import sys

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

cnt = collections.Counter(d)

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

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

for i in range(1,max(d)+1):
    p = (p * pow(cnt[i-1], cnt[i], 998244353))%998244353

print(p)

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

list2 = [0]*(max(list1)+1)

for i in range(len(list1)):
    list2[list1[i]] += 1

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


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

if 0 in list2:
    print(0)
    exit()

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

print(count)

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

if d[0] != 0:
    print(0)
    exit()
count = [0 for _ in range(max_d+1)]
for i in range(n):
    count[d[i]] += 1
if count[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(max_d):
    n = count[i]
    m = count[i+1]
    ans *= n**m%mod
print(ans%mod)

Solution (Python):
def calc(n, D):
    if D[0] != 0:
        return 0
    count = [0 for i in range(n)]
    for i in D:
        count[i] = count[i] + 1
    if count[0] != 1:
        return 0
    
    
    tmp = 1
    sum = 0
    for i in range(n-1):
    
        sum = sum + count[i]
        if sum == n:
            break
        if count[i] == 0:
            tmp = 0
            break
        tmp = tmp * (count[i]**count[i+1])
    return tmp

n = int(input())

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

print(calc(n,D) % 998244353)

Solution (Python):
import collections

MOD = 998244353
n = int(input())
d = list(map(int, input().split()))
if d[0] != 0:
    print(0)
    exit()

d.sort()

c = collections.Counter(d)
if c[0] != 1:
    print('0')
else:
    ans = 1
    k = list(c.keys())
    v = list(c.values())
    for i in range(1, len(v)):
        if k[i]-1 != k[i-1]:
            ans *= 0
        ans *= (v[i-1]**v[i]) % MOD
    print(ans % MOD)

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

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

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

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

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

cnt = 1
for i in range(1,max(d)+1):
    cnt *= pow(c[i-1],c[i])

print(cnt % MOD)

Solution (Python):
from sys import stdin
import collections
def main():
    n = int(stdin.readline().rstrip())
    D = [int(i) for i in input().split()]
    hash = collections.Counter(D)
    if(hash[0] != 1 or D[0] != 0):
        print(0)
        return
    count = hash[0]
    for i in range(1, max(D)+1):
        count *= hash[i-1] ** hash[i]
    print(count%998244353)
    
if __name__ == '__main__':
    main()

Solution (Python):
import collections
import sys

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

cnt = collections.Counter(d)

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

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

for i in range(1,max(d)+1):
    if cnt[i] == 0:
        print(0)
        sys.exit(0)  
    else:
        p = (p * pow(cnt[i-1], cnt[i], 998244353))%998244353

print(p)

Solution (Python):
from sys import stdin
import collections
def main():
    n = int(stdin.readline().rstrip())
    D = [int(i) for i in input().split()]
    hash = collections.Counter(D)
    count = hash[0]
    if(count != 1 or D[0] != 0):
        print(0)
        return
    for i in range(1, max(D)+1):
        count *= hash[i-1] ** hash[i]
    print(count%998244353)
    
if __name__ == '__main__':
    main()