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



def main():
    from collections import Counter

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

    
    

    
    
    

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

    ans = 1
    mod = 998244353

    for i in c.keys():
        if i >= 1:
            ans *= c[i - 1] ** c[i]
            ans %= mod

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import Counter


def main():
    n = int(input().strip())
    d = list(map(int, input().strip().split()))
    if d[0] != 0:
        print(0)
        return
    e = sorted(list(set(d)))
    for i, v in enumerate(e):
        if i != v:
            print(0)
            return
    v = dict(Counter(d))
    if v[0] != 1:
        print(0)
        return
    i = 0
    ans = 1
    while True:
        if i in v:
            if i == 0:
                i += 1
                continue
            ans *= v[i-1] ** v[i]
            ans %= 998244353
        else:
            break
        i += 1
    print(ans)

if __name__ == '__main__':
    main()

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)):
        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
input = sys.stdin.readline

def main():
  N = int(input())
  vD = list(map(int, input().split()))
  MOD = 998244353

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

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

  res = 1
  p = -1
  pcnt = 1
  ccnt = 1
  for d in vD:
    if d==p+1:
      pcnt = ccnt
      ccnt = 1
    elif d==p:
      ccnt += 1
    else: 
      print(0)
      return

    res *= pcnt
    res %= MOD
    p = d

  print(res)

main()

Solution (Python):
from collections import Counter

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

if d[0] != 0 or min(d[1:]) == 0:
    print(0)
else:
    c = Counter(d)
    ans = 1
    for i in range(1, max(d)+1):
        ans *= c[i-1] ** c[i]
        ans = ans % mod
    print(ans)

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

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

c=Counter(d)
d_max=max(d)
ans=1
flag=True

if d[0]!=0:
    print(0)
elif d.count(0)>1:
    print(0)
else:
    for i in range(1, d_max+1):
        if c[i-1]==0:
            flag=False
        else:
            ans=ans*(c[i-1]**c[i])%998244353

    print(ans%998244353 if flag else 0)

Solution (Python):
from collections import Counter

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

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

Solution (Python):
from collections import Counter

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

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

Solution (Python):

n1 = int(input()) - 1
d = list(map(int, input().strip().split()))
mod = 998244353
if d[0] != 0 or 0 in d[1:]:
    print(0)
else:
    max_path = max(d)
    flag = True
    v = [0 for i in range(max_path)]
    for i in d[1:]:
        v[i-1] += 1

    if 0 in v:
        flag = False

    if flag:
        num = 1
        hh = 1

        def binpow(a, b):
            if b == 0:
                return 1
            elif a == 1:
                return 1
            elif b % 2 == 0:
                c = binpow(a, b // 2)
                return (c ** 2) % mod
            else:
                return (binpow(a, b - 1) * a) % mod


        for h in v:
            num *= binpow(hh, h)
            num = num % mod
            hh = h

        print(num)

    else:
        print(0)

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

Solution (Python):

n1 = int(input()) - 1
d = list(map(int, input().strip().split()))
mod = 998244353
if d[0] != 0 or 0 in d[1:]:
    print(0)
else:
    max_path = max(d)
    flag = True
    v = [0 for i in range(max_path)]
    for i in d[1:]:
        v[i-1] += 1

    if 0 in v:
        flag = False

    if flag:
        num = 1
        hh = 1

        for h in v:
            num *= hh ** h
            num = num % mod
            hh = h

        print(num)

    else:
        print(0)

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

Solution (Python):


import sys
import math
import collections

N, = map(int, sys.stdin.readline().rstrip().split(" "))
Ds = list(map(int, sys.stdin.readline().rstrip().split(" ")))
if Ds.pop(0) != 0:
    print(0)
else:
    Ds.sort()
    leaf_count = collections.defaultdict(int)
    leaf_count[0] = 1
    result = 1
    for D in Ds:
        if leaf_count[D - 1] == 0:
            result = 0
            break
        else:
            result *= leaf_count[D - 1]
            result %= 998244353
            leaf_count[D] += 1
    print(result)
exit(0)

Solution (Python):

import collections

n = int(input())

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

waru = 998244353

c = collections.Counter(d)

ans =1

cmax =max(c.keys())
if d[0] != 0:
    print(0)
else:
    for i in range(1,cmax+1):
        if d[i] == 0:
            ans = 0
            break
        ans *= c[i-1]** c[i]

    print(ans % waru)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
mod = 998244353
if a[0] != 0:
  print(0)
  exit()
cnt = {}
for e in a:
  cnt[e] = cnt.get(e, 0) + 1
if cnt[0] > 1:
  print(0)
  exit()
d, c = 0, cnt[0]
ans = 1
for k, v in sorted(list(cnt.items())):
  
  if k == 0:
    continue
  if k > d + 1:
    print(0)
    exit()
  for i in range(v):
    ans = ans * c % mod
  d, c = k, v
print(ans)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
q=[0]*(max(d)+1)




for i in range(len(d)):
    q[d[i]]+=1
    
if q[0]!=1 or d[0]!=0:
    print(0)
    exit()

ans=1
for i in range(1,max(d)+1):
    ans*=q[i-1]**q[i]
    if ans == 0:
        break
print(ans%998244353)

Solution (Python):
from collections import Counter

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

if DD[0] != 0:
    print(0)
elif D[0] != 1:
    print(0)
else:
    ans = 1
    for i in range(len(D)-1):
        ans *= D[i]**D[i+1]
        ans %= 998244353 
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

    count = [0] * N
    for d in D:
        count[d] += 1
    
    if D[0] != 0 or count[0] >= 2:
        print(0)
        exit()

    ans = 1
    for i in range(N):
        if i > 0:
            ans *= (count[i-1] ** count[i]) % 998244353
            ans %= 998244353

    print(ans)

    

if __name__ == '__main__':
        main()

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

def main():
    _ = int(input())
    D = list(map(int, input().split()))
    Dnum = Counter(D)
    if D[0] != 0 or Dnum[0] != 1:
        print(0)
        exit()
    ans = 1
    for i in range(1, max(D)+1):
        ans *= Dnum[i] ** Dnum[i+1]
        if ans == 0:
            break
    print(ans%998244353)


if __name__ == '__main__': main()

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

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(d)+1):
    ans*=q[i-1]**q[i]
    if ans == 0:
        break
print(ans%998244353)

Solution (Python):
n = int(input())
d = list(map(int,input().split()))
 
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(d)+1):
  ans *= q[i-1]**q[i]
  if ans == 0:
    break
print(ans%998244353)

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

f=True

if D[0]!=0:
	f=False
D.sort()

d={}
a=0
for i in range(N):
	if D[i] not in d:
		if D[i]==a:
			d[D[i]]=1
			a=D[i]+1
		else:
			f=False
			break
	else:
		d[D[i]] += 1

if not(f):
	print(0)
elif d[0]>1:
	print(0)
else:
	ans=1
	d = list(d.values())
	for i in range(1, len(d))[::-1]:
		ans *= (d[i-1]**d[i])%998244353
		ans %= 998244353
	print(ans)

Solution (Python):
import collections

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

c = collections.Counter(d)

ans = 1

if c[0] != 1 or d[0] !=0:
    ans = 0

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

print(ans%998244353)

Solution (Python):
n = int(input())
d = list(map(int,input().split()))
 
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(d)+1):
  ans *= q[i-1]**q[i]
  if ans == 0:
    break
print(ans%998244353)

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

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

if d[0] != 0 or min(d[1:]) == 0:
	print(0)
else:
	ans = 1
	c = Counter(d)
	for k, v in c.items():
		if k == 0:
			continue
		ans *= pow(c[k-1], v, MOD)
		ans %= MOD
	print(ans)

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

from collections import Counter
q=Counter(d)

ans = 1
if q[0]!=1 or d[0]!=0:
    print(0)
    exit()
d.sort()
for i in range(1,max(d)+1):
  ans *= q[i-1]**q[i]
  if ans == 0:
    break
print(ans%998244353)

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
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):
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:]) :
    print(0)
    exit()
t = 1
r = 1
for x in m:
    t = t * pow(r,x,mod) % mod
    r = x

print(t)

Solution (Python):



def solve(*args: str) -> str:
    _ = int(args[0])
    D = list(map(int, args[1].split()))

    max_d = max(D)

    nodes = [0]*(max_d+1)
    for d in D:
        nodes[d] += 1

    ret = 0
    if D[0] == 0 and nodes.count(0) == 0 and nodes[0] == 1:
        ret = 1
        for i in range(2, max_d+1):
            ret *= pow(nodes[i-1], nodes[i], 998244353)

    return str(ret % 998244353)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
maxd=max(d)
import collections
counter=collections.Counter(d)
if d[0]!=0 or counter[0]!=1:
  print(0)
else:
  a=1
  ans=1
  for i in range(1,maxd+1):
    if i not in counter:
      print(0)
      break
    else:
      b=counter[i]
      ans*=(a**b)
      a=b
  else:
    print(ans%998244353)

Solution (Python):
from collections import Counter
from functools import reduce
n = int(input())
d = (int(s) for s in input().split())
if next(d) != 0:
    print(0)
    quit(0)
c = Counter(d)
if c[0] != 0:
    print(0)
    quit(0)
print(reduce(lambda x, y: x * y, (c[k - 1] ** x if k > 1 else 1 for k, x in c.items()), 1) % 998244353)

Solution (Python):
import numpy as np
import collections
input1 = list(map(int,input().split()))
input2 = list(map(int,input().split()))

N = input1[0]
D = input2

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

counter = collections.Counter(D)

ans = 1

D.sort()
D_numpy = np.array(D)
if (np.diff(D_numpy)<=1).prod():
  if counter[0] != 1:
    ans = 0
  else:
    for i in range(len(counter)):
      ans *= counter[i]**counter[i+1]
      if ans >= 998244353:
        ans = ans%998244353

else:
  ans = 0



print(ans)

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


count = [0] * N
for d in D:
    count[d] += 1

if (D[0] != 0 or count[0] >= 2):
    print(0)
    exit()
ans = 1
for i in range(N):
    if (i > 0):
        ans *= (count[i - 1] ** count[i]) % 998244353
        ans %= 998244353

print(ans)

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

count = [0] * N
for d in D:
    count[d] += 1

if (D[0] != 0 or count[0] >= 2):
    print(0)
    exit()
ans = 1
for i in range(N):
    if (i > 0):
        ans *= (count[i - 1] ** count[i]) % MOD
        ans %= MOD

print(ans)

Solution (Python):


















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


count = [0] * N
for d in D:
    count[d] += 1


if D[0] != 0 or count[0] >= 2:
    print(0)
    exit()
ret = 1
for i in range(N):
    if i > 0:
        ret *= (count[i-1] ** count[i]) % MOD
        ret %= MOD
print(ret)

Solution (Python):


















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


count = [0] * N
for d in D:
    count[d] += 1

def calc():
    if D[0] != 0 or count[0] >= 2:
        return 0
    ret = 1
    for i in range(N):
        if i > 0:
            ret *= (count[i-1] ** count[i]) % MOD
            ret %= MOD
    return ret


print(calc())

Solution (Python):
import numpy as np
NUMBER = 998244353
 
N, *D = map(int, open(0).read().split())
bincnt = np.bincount(D)
if D[0] != 0:
    print('0')
elif (bincnt==0).sum() > 0:
    print('0')
elif bincnt[0]!=1:
    print('0')
else:
    val = 1
    for i in range(2, bincnt.shape[0]):
        for _ in range(bincnt[i]):
            val = (val*bincnt[i-1]%NUMBER)
    print(val)

Solution (Python):
import numpy as np
NUMBER = 998244353
 
N = int(input())
D = list(map(int, input().split()))
bincnt = np.bincount(D)
if D[0] != 0:
    print('0')
elif (bincnt==0).sum() > 0:
    print('0')
elif bincnt[0]!=1:
    print('0')
else:
    val = 1
    for i in range(2, bincnt.shape[0]):
        for _ in range(bincnt[i]):
            val = (val*bincnt[i-1]%NUMBER)
    print(val)

Solution (Python):

import sys
sys.setrecursionlimit(1000000)


def dfs(now, end, summary):
    
    if now == end:
        return summary
    
    else:
        return dfs(now + 1, end, summary * (deep[now - 1] ** deep[now]))


JOSU = 998244353


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



deep = [0] * (max(D) + 1)


for i in range(N):

    
    if i == 0 and D[i] != 0:
        print(0)
        exit()
    
    elif i > 0 and D[i] == 0:
        print(0)
        exit()

    deep[D[i]] += 1


print(dfs(1, len(deep), 1) % JOSU)

Solution (Python):
BIGMOD = 998244353
 
def modPow(x,n):
    ans = 1
    while n >= 1:
        if bin( n & 1)==bin(1):
            ans = (ans*x) % BIGMOD
        x = x * x % BIGMOD
        n = n>>1
    return ans % BIGMOD
 
def modFact(N):
    fact = []
    fact.append(1) 
    for i in range(1,N+1):
        a = fact[i-1]*i % BIGMOD
        fact.append(a)
    return fact
 
def modInvFact(fact):
    invFact = []
    for i in range(1,N+2):
        a = modPow(fact[i-1],BIGMOD-2)
        invFact.append(a)
    return invFact
 
def modPlus(a,b):
    return (a + b) % BIGMOD
 
def modMinus(a,b):
    if a-b >= 0:
        return (a - b) % BIGMOD
    else:
        return (a - b + BIGMOD)
    
def modMulti(a,b):
    return a * b % BIGMOD
 

def nCr(fact,invFact,n,r):
    return (fact[n] * invFact[r] * invFact[n-r] ) %BIGMOD
 
def nPr(fact,invFact,n,r):
    return(fact[n] * invFact[r])%BIGMOD
 
def fact(num):
    ans = 1
    for i in range(1,num+1):
        ans = ans*i%BIGMOD
    return ans
 
import collections
 
N = int(input())
D = list(map(int,input().split()))
 
S = list(set(D))
S.sort()
if S[0]==0:
    S.pop(0)
ans = 1
before = 0
before_i = 0
c = collections.Counter(D)
if c[0] > 1:
  ans=0
for i in S:
    if i == 1:
        before = c[i]
        before_i = i
        continue
    ans = ans*modPow(before,c[i])%BIGMOD
    before = c[i]
    if i - before_i!=1:
        ans = 0
    before_i = i
if D[0] >0:
    print(0)
else:
    print(ans)

Solution (Python):
from collections import defaultdict
n=int(input())
d=[int(i) for i in input().split()]
mod = 998244353

if d[0]!=0:
    print(0)    
    exit()
for i in d[1:]:
    if i==0:
        print(0)
        exit()
    
    
d=sorted(d)
ans = 1
    
dic = defaultdict(int)
dic[0] = 1
for i in d[1:]:
    ans *= dic[i-1]
    ans = ans % mod
    dic[i] += 1
print(ans)

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


count = [0] * N
for d in D:
    count[d] += 1

def calc():
    if D[0] != 0 or count[0] >= 2:
        return 0
    ret = 1
    for i in range(N):
        if i > 0:
            ret *= (count[i-1] ** count[i]) % MOD
            ret %= MOD
    return ret

print(calc())

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


count = [0] * N
for d in D:
    count[d] += 1

def calc():
    if D[0] != 0:
        return 0
    ret = 1
    for i in range(N):
        if i == 0:
            if count[i] >= 2:
                return 0
        else:
            ret *= (count[i-1] ** count[i]) % MOD
            ret %= MOD
    return ret

print(calc())

Solution (Python):
import collections
 
N=int(input())
D=list(map(int,input().split()))
 
c=collections.Counter(D)
 
max_D=max(D)
ans=1
previous_leaf=1
 
if D[0]!=0:
    ans=0
elif c[0]!=1:
    ans=0
else:
    for i in range(1,max_D+1):
        ans*=(previous_leaf**c[i])
        ans%=998244353
        previous_leaf=c[i]
 
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())
    D=list(map(int,input().split()))
    M=max(D)

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

    C=[0]*(M+1)
    for d in D:
        C[d]+=1

    if(C[0]!=1):
        print(0)
        return

    if(any(C[i]==0 for i in range(M+1))):
        print(0)
        return

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

Solution (Python):
n,*d=map(int,open(0).read().split())
e=[0]*n
for i in d:e[i]+=1
a=d[0]<1==e[0]
for x,y in zip(e,e[1:]):a=a*x**y%998244353
print(a)

Solution (Python):
_,d=open(0)
*d,=map(int,d.split())
e=[0]*10**5
for i in d:e[i]+=1
a=d[0]<1==e[0]
for x,y in zip(e,e[1:]):a=a*x**y%998244353
print(a)

Solution (Python):
_,d=open(0)
*d,=map(int,d.split())
e=[0]*-~max(d)
for i in d:e[i]+=1
a=d[0]<1==e[0]
for x,y in zip(e,e[1:]):a*=x**y
print(a%998244353)

Solution (Python):
_,d=open(0)
*d,=map(int,d.split())
e=[0]*-~max(d)
for i in d:e[i]+=1
a=d[0]<1==e[0]
for x,y in zip(e,e[1:]):a*=x**y
print(a%998244353)

Solution (Python):
import collections

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

c=collections.Counter(D)

max_D=max(D)
ans=1
previous_leaf=1

if D[0]!=0:
    ans=0
elif c[0]!=1:
    ans=0
else:
    for i in range(1,max_D+1):
        ans*=(previous_leaf**c[i])
        ans%=998244353
        previous_leaf=c[i]

print(ans)

Solution (Python):
mod=998244353
def modpow(a,p):
    res=1
    for i in range(p):
        res*=a
        res%=mod
    return res

n=int(input())
d=input().split(" ")
a={}
for i in range(0,n):
    a.setdefault(i,0)
for i in d:
    a[int(i)]+=1
ans=1
if d[0]!='0' or a[0]!=1:
    ans=0
else:
    for i in range(n-1):
        ans*=modpow(a[i],a[i+1])
        ans%=mod

print(ans)

Solution (Python):
from collections import Counter
from itertools import tee

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


def pairwise(iterable):
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)


def is_leap(distance):
    cc = Counter(distance)
    for x, y in pairwise(sorted(cc.keys())):
        if y - x > 1:
            return True
    else:
        return False


if D[0] > 0:
    """頂点1が根ではない"""
    print(0)
elif not all(D[1:]):
    """連結グラフではない"""
    print(0)
elif is_leap(D):
    """連結グラフではない"""
    print(0)
else:
    ans = 1
    c = Counter(D[1:])
    edges = [t[1] for t in sorted(c.items())]
    for d, e in pairwise(edges):
        ans *= pow(d, e)
        ans = ans % R
    print(ans % R)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
cnt = [0]*n
for i in a:
    cnt[i] += 1

output = 1
if a[0] != 0 or cnt[0] != 1:
    print(0)
elif max(a) == 1:
    print(1)
else:
    for i in range(2, n):
        output *= cnt[i-1]**cnt[i]
        output %= 998244353
    print(output)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline())
def LI(): return [int(x) for x in sys.stdin.readline().split()]
mod = 998244353


def main():
    N = I()
    A = LI()

    if A[0] != 0:
        return 0

    DistCnt = [0] * (max(A)+1)
    for i in range(N):
        DistCnt[A[i]] += 1

    if DistCnt[0] != 1:
        return 0

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

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

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

num_dict = {}
ans = 1

for i in range(N):
  num_dict[D[i]] = num_dict.get(D[i],0) + 1

if num_dict.get(0) == None or num_dict.get(0) >= 2 or D[0] != 0:
  ans = 0
else:
  for i in range(1,len(num_dict)):
    if num_dict.get(i) == None:
      ans = 0
      break
    else:
      ans *= num_dict.get(i-1) ** num_dict.get(i)

print(ans%998244353)

Solution (Python):
N = int(input())
D = list(map(int, input().split()))
M = [0]*N
for d in D: M[d]+=1

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

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

print(ans)

Solution (Python):

from collections import Counter


def main():
    N, *D = map(int, open(0).read().split())
    MOD = 998244353
    cnt = Counter(D)
    if D[0] != 0 or cnt[0] > 1:
        ans = 0
    else:
        ans = 1
        for i in D[1:]:
            ans = (ans * cnt[i - 1]) % MOD
    print(ans)


if __name__ == "__main__":
    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 = 1
  try:
    for i in range(1, n + 1):
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p
  except:
    pass
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 = 1
  for i in range(1, n + 1):
    x = c[i]
    if x == 0:
      break
    r = r * pow(p, x, M) % M
    p = x; n -= p
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 = 1
  try:
    for i in range(1, n + 1):
      r = r * pow(p, c.get(i), M) % M
      p = c[i]; n -= p
  except:
    pass
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 = 1
  try:
    for i in range(1, n + 1):
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p
  except:
    pass
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 = 1
  try:
    for i in range(1, n + 1):
      if n < 2:
        break
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p
  except:
    pass
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 = 1
  try:
    for i in range(1, n + 1):
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p
  except:
    pass
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
  try:
    while n > 1:
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p; i += 1
  except:
    pass
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 = 1
  try:
    for i in range(1, n + 1):
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p
  except:
    pass
print((r, 0)[n > 1])

Solution (Python):
from collections import Counter
from itertools import count

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 = 1
  try:
    for i in count(1):
      x = c.get(i)
      r = r * pow(p, x, M) % M
      p = x; n -= p
  except:
    pass
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
    try:
        while True:
            x = c.get(i)
            r = r * pow(p, x, M) % M
            p = x; n -= p; i += 1
    except:
        pass
print((r, 0)[n > 1])

Solution (Python):
import sys

N = int(input())

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

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

countl = [0]
D = list(sorted(D))
if not check == D[0]:
    print(0)
    sys.exit()
countl = [0]
for d in D:
    if not check == d:
        check += 1
        countl.append(0)
        if not check == d:
            print(0)
            sys.exit()
    countl[-1] += 1

if not countl[0] == 1:
    print(0)
    sys.exit()

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

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 True:
    x = c.get(i, 0)
    if x == 0:
      break
    r = r * pow(p, x, M) % M
    p = x; 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 True:
    x = c[i]
    if x == 0:
      break
    r = r * pow(p, x, M) % M
    p = x; 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
    try:
        while True:
            x = c.get(i)
            r = r * pow(p, x, M) % M
            p = x; n -= p; i += 1
    except:
        pass
print((r, 0)[n > 1])

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


lst = [[] for i in range(n)]
ans = 1
sum = 1
P = 998244353
if d[0] != 0:
    print(0)
    quit()
else:
    for i in range(n):
        if d[i] == 0 and i != 0:
            print(0)
            quit()
        lst[d[i]].append(i + 1)

    tmp = len(lst[1])
    for i in range(1, n):
        if len(lst[i]) == 0:
            if sum != n:
                print(0)
                quit()
        sum += len(lst[i])
        if i == 1:
            continue
        ans = (ans * tmp ** len(lst[i])) % P
        tmp = len(lst[i])
    print(ans)