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
from operator import itemgetter
N = int(input())
D = list(map(int, input().split()))
MOD = 998244353

class Mint:
    @staticmethod
    def get_value(x): return x.value if isinstance(x, Mint) else x

    def __init__(self, val, mod=10**9+7):
        val = Mint.get_value(val)
        self.__mod = mod
        self.__val = val % self.__mod

    @property
    def value(self): return self.__val
    @property
    def inverse(self): return pow(self.__val, self.__mod - 2, self.__mod)

    def __eq__(self, other): return self.__val == other.val
    def __ne__(self, other): return self.__val != other.val

    def __neg__(self): return Mint(self.__val, self.__mod)

    def __iadd__(self, other):
        other = Mint.get_value(other)
        self.__val = (self.__val + other) % self.__mod
        return self
    def __add__(self, other):
        new = Mint(self.__value, self.__mod)
        new += other
        return new
    def __radd__(self, other): return self + other

    def __isub__(self, other):
        other = Mint.get_value(other)
        self.__val = (self.__val - other + self.__mod) % self.__mod
        return self
    def __sub__(self, other):
        new = Mint(self.__value, self.__mod)
        new -= other
        return new
    def __rsub__(self, other):
        new = Mint(Mint.get_value(other), self.__mod)
        new -= self
        return new

    def __imul__(self, other):
        other = Mint.get_value(other)
        self.__val = self.__val * other % self.__mod
        return self
    def __mul__(self, other):
        new = Mint(self.__value, self.__mod)
        new *= other
        return new
    def __rmul__(self, other): return self * other

    def __ifloordiv__(self, other):
        other = Mint(other, self.__mod)
        self *= other.inverse
        return self
    def __floordiv__(self, other):
        new = Mint(self.__value, self.__mod)
        new //= other
        return new
    def __rfloordiv__(self, other):
        new = Mint(Mint.get_value(other), self.__mod)
        new //= self
        return new

C = Counter(D)

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

ans = Mint(1, MOD)
expected = 0
for i,c in sorted(C.items(), key=itemgetter(0)):
    if i == 0: continue
    expected += 1
    if i != expected:
        print(0)
        exit()
    ans *= pow(C[i-1], c, MOD)

print(ans.value)

Solution (Python):
mod = 998244353
N = int(input())
D = [int(i) for i in input().split()]
if D[0] != 0:
    print(0)
    exit()
for i in range(1,N):
    if D[i] == 0:
        print(0)
        exit()
def pow_k(a,n,mod): 
    if n == 0:
        return 1
    if n % 2 ==0:
        return pow_k(a*a % mod,n//2,mod)
    else:
        return a * pow_k(a,n-1,mod) % mod
import collections
c = collections.Counter(D)
li = list(c.keys())
li.sort()
n = li[-1]
ans = 1
for i in range(1,n+1):
    if c[i] == 0:
        print(0)
        exit()
    else:
        j = i-1
        k = pow_k(c[j],c[i],mod)
        ans *= k
        ans %= mod
print(ans)

Solution (Python):
import collections

mod=998244353
n=int(input())
arr=list(map(int,input().split()))
if arr[0]!=0:
  print(0)
else:
  count=collections.Counter(arr)
  if count[0]!=1:
    print(0)
  else:
    ans=1
    for i in range(1,n):
      ans*=pow(count[i-1],count[i],mod)
      ans%=mod
    print(ans)

Solution (Python):
MOD=998244353
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self //value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

from collections import Counter
N,*D=map(int, open(0).read().split())
ans=Fp(D[0]==0 and 0 not in D[1:])
C=Counter(D)
for k, v in sorted(C.items())[1:]:
    ans*=Fp(C[k-1])**v if k-1 in C else 0
print(ans)

Solution (Python):

from collections import Counter
n = int(input())
lst = list(map(int,input().split()))
if lst[0] != 0:
    print(0)
    exit()
mod = 998244353
c = dict(Counter(lst))
c = dict(sorted(c.items(), key=lambda x:x[0]))

for e,(k,v) in enumerate(c.items()):
    if e==0:
        if k!=0 or v!=1:
            ans = 0
            break
        else:
            pre_v = v
            ans = v
    elif e!=k or v==0:
        ans = 0
        break
    else:
        ans = ans*pow(pre_v, v, mod)%mod
        pre_v = v
print(ans%mod)

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

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

far = 0

cnt = [0 for i in range(N)]

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

    cnt[D[i]] += 1
    far = max(D[i], far)

ans = 1

for i in range(1, far):
    ans *= cnt[i] ** cnt[i + 1]

print(ans % 998244353)

Solution (Python):
from collections import Counter

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

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

ans = 1
for i in range(len(cnt)+1):
    ans *= pow(cnt[i], cnt[i+1], mod)
    ans %= mod
print(ans)

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

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

d.sort()
import collections
c = collections.Counter(d)

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

print(ans)

Solution (Python):
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
mod = 998244353
if a[0] != 0:
  print(0)
  exit()
ans = 1
c = Counter(a)
cls = list(c.items())
cls.sort()
if cls[0][1] != 1:
  print(0)
  exit()
for i in range(1,len(cls)):
  if cls[i][0] != i:
    print(0)
    exit()
  ans = ans*cls[i-1][1]**cls[i][1]%mod
print(ans)

Solution (Python):
mod = 998244353
N = int(input())
D = list(map(int,input().split()))
from collections import Counter
if D[0] != 0 or 0 in D[1:]:
    print(0)
else:
    dc1 = Counter(D[1:])
    dc = [0 for i in range(max(D))]
    for k,v in dc1.items():
        dc[k-1] = v
    ans = 1
    for i in range(1,len(dc)):
        if dc[i] == 0:
            ans = 0
            break
        ans = (ans*(dc[i-1]**dc[i]))%mod
    print(ans)

Solution (Python):
from collections import Counter

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

C = Counter(D)

if D[0] > 0 or C[0] > 1:
  print(0)
  exit(0)
  
mod = int(998244353)
ans = 1
for i in range(2,max(D)+1):
  ans = (ans * (C[i-1] ** C[i]) % mod) % mod
  
print(ans)

Solution (Python):
def main():
  import sys
  n,*d=map(int,sys.stdin.buffer.read().split())
  e=[0]*n
  
  for i in d:
    e[i]+=1
  
  a=e[0]
  
  for i in d[1:]:
    a=a*e[i-1]%998244353
    
  print(a)
  
if __name__=="__main__":
  main()

Solution (Python):
N = int(input())
D = list(map(int, input().split()))
ans = 0
if not D[0] == 0 or not D.count(0) == 1:
    print(0)
else:
    co = [0] * (max(D)+1)
    for i in D:
        co[i] += 1
    ans = 1
    for i in range(2, max(D)+1):
        ans = ans * (co[i-1]**co[i]) % 998244353
    print(ans)

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

Solution (Python):

import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = 998244353
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    
    N = Scanner.int()
    D = Scanner.map_int()
    if D[0] != 0:
        print(0)
        return
    if D.count(0) != 1:
        print(0)
        return
    C = Counter(D)
    ans = 1
    for i in range(1, len(C) + 1):
        ans *= C[i-1] ** C[i]
        ans %= MOD
    print(ans)
    return


if __name__ == "__main__":
    main()

Solution (Python):


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

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

MOD = 998244353

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

key = list(c.keys())
key.sort()

ans = 1
for k in key:
    if k >= 1:
        if k-1 not in c:
            print(0)
            exit()
        else:
            ans *= c[k-1] ** c[k]
            ans %= MOD

print(ans)

Solution (Python):
import bisect

n = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
  print(0)
else:
  c = 1
  temp = 1
  e = 1
  D.sort()
  m = D[-1]
  if n > 1 and D[1] == 0:
    print(0)
    exit()
  else:
    for i in range(1, m+1):
      a = bisect.bisect_right(D, i)
      d = a - temp
      temp = a
      if d == 0 and i>0:
        print(0)
        exit()
      else:
        c *= e**d
        c %= 998244353
        e = d
    print(c)

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

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

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

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

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

print(ans%998244353)

Solution (Python):
def examA():
    N = I()
    ans = (N-1)//2
    print(ans)
    return

def examB():
    N = I()
    D = LI()
    d = Counter(D)
    loop = max(d.keys())
    if D[0]!=0 or d[0]!=1:
        print(0)
        return

    ans = 1; cur = 1
    for i in range(loop+1):
        if d[i]==0:
            print(0)
            return
        for _ in range(d[i]):
            ans *= cur
            ans %= mod2
        cur = d[i]
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

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

nodes = [0] * N

for d in D:
  nodes[d] += 1

ans = 0
if nodes[0]==1 and D[0]==0:
  pre_n = 1
  ans = 1
  for n in nodes:
    ans = (ans * pre_n**n) % MOD
    pre_n = n

print(ans)

Solution (Python):
def main():
  import sys
  n,*d=map(int,sys.stdin.buffer.read().split())
  e=[0]*n
  for i in d:e[i]+=1
  a=1if d[0]<1else 0
  for i in d[1:]:a=a*e[i-1]%998244353
  print(a)
main()

Solution (Python):
def main():
  n,*d=map(int,open(0).read().split())
  e=[0]*n
  for i in d:e[i]+=1
  a=1if d[0]<1else 0
  for i in d[1:]:a=a*e[i-1]%998244353
  print(a)
main()

Solution (Python):
def main():
  from collections import Counter
  M=998244353
  n,*d=map(int,open(0).read().split())
  e=Counter(d)
  a=1if d[0]<1==e[0]else 0
  for i in range(n):a*=pow(e[i],e[i+1],M)
  print(a%M)
main()

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

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

ans = 1
for d in D[1:]:
    ans = ans * counter[d-1] % mod
print(ans)

Solution (Python):
import sys
read = lambda: sys.stdin.readline().rstrip()

class Solution(object):
    def counting_tree(self, N, D):
        
        tot = 1

        

        cnt = [0]*N
        for i in D:
            cnt[i]+=1

        for i in D[1:]:
            tot = tot * cnt[i-1]%998244353

        return tot if D[0]==0 else 0

def main():
    
    N0 = int(read())
    D0 = tuple(map(int, read().split()))

    res = Solution().counting_tree(N0, D0)

    print(res)

if __name__ == "__main__":
    main()

Solution (Python):
from collections import defaultdict



def power(a, n, p):
    bi = str(format(n, "b"))  
    res = 1
    for i in range(len(bi)):
        res = (res * res) % p
        if bi[i] == "1":
            res = (res * a) % p
    return res


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

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

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

ans = 1
for i in range(1, max(D) + 1):
    if cnt.get(i) is None:
        print(0)
        exit(0)
    num = pow(cnt[i - 1], cnt[i], MOD)
    ans = ans * num % MOD
print(ans)

Solution (Python):
import sys
read = lambda: sys.stdin.readline().rstrip()

def counting_tree(N, D):
    import collections
    tot = 1

    cnt = collections.Counter(D)
    """
    cnt = [0]*N
    for i in D:
        cnt[i]+=1
    """

    for i in D[1:]:
        tot = tot * cnt[i-1]%998244353

    return tot if D[0]==0 else 0

def main():
    
    N0 = int(read())
    D0 = tuple(map(int, read().split()))

    res = counting_tree(N0, D0)

    print(res)

if __name__ == "__main__":
    main()

Solution (Python):
import sys
read = lambda: sys.stdin.readline().rstrip()

def counting_tree(N, D):
    tot = 1
    
    
    cnt = [0]*N
    for i in D:
        cnt[i]+=1

    for i in D[1:]:
        tot = tot * cnt[i-1]%998244353

    return tot if D[0]==0 else 0

def main():
    
    N0 = int(read())
    D0 = tuple(map(int, read().split()))

    res = counting_tree(N0, D0)

    print(res)

if __name__ == "__main__":
    main()

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

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

cnt = [0] * n

for i in li:
    cnt[i] += 1

ans = 1

for i in li[1:]:
    ans = ans*cnt[i-1]%998244353
if li[0] != 0:
    ans = 0

print(ans)

Solution (Python):
a = int(input())
l = list(map(int,input().split()))
d = [0]*a
for i in l:
    d[i]+=1
r =1
if  l[0]!=0:
    print(0)
    exit()
for i in l[1:]:
    r=r*d[i-1]%998244353
print(r)

Solution (Python):
a = int(input())
l = list(map(int,input().split()))
MOD = 998244353
dp = [0]*a
for i in l:
    dp[i]+=1
res = 1

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

for i in l[1:]:
    
    res*=dp[i-1]
    res%=MOD

print(res%MOD)

Solution (Python):
a = int(input())
l = list(map(int,input().split()))
MOD = 998244353
dp = [0]*a
for i in l:
    dp[i]+=1
res = 1

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

for i in l[1:]:
    
    res*=dp[i-1]
    res%=MOD

print(res)

Solution (Python):
n = int(input())
d = [int(i) for i in input().split()]
m = max(d)
cnt = [0]*(m+1)
ans = 1
flag = True
mod = 998244353

if d[0] == 0:
    cnt[0] += 1
else:
    flag = False

for i in range(1,n):
    if d[i] == 0:
        flag == False
        break
    else:
        cnt[d[i]] += 1

for i in range(1,m+1):
    ans *= cnt[i-1]**cnt[i]
    ans %= mod

if flag == False:
    ans = 0

print(ans)

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


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


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False



def main():
    MOD = 998244353
    N = I()
    D = LI()

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

    if any([d == 0 for d in D[1:]]):
        print(0)
        return

    MAX_D = max(D)
    node = [0] * (MAX_D + 1)

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import collections

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

if D[0]==0 and data[0]==1:
    count=1
    for i in range(1,len(data)):
        count*=data[i-1]**data[i]
        count%=mod
    print(count)
else:
    print(0)

Solution (Python):
import collections

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

if D[0]==0 and data[0]==1:
    count=1
    for i in range(1,len(data)):
        count*=data[i-1]**data[i]
        count%=mod
    print(count)
else:
    print(0)

Solution (Python):
n = int(input())
lis = list(map(int,input().split()))
num = [0 for i in range(max(lis)+1)]
if lis[0] != 0:
    print(0)
    exit()
if lis.count(0) > 1:
    print(0)
    exit()
for nu in lis:
    num[nu] += 1
ans = 1
for i in range(max(lis)):
    if num[i] == 0:
        print(0)
        exit()
    for _ in range(num[i+1]):
        ans *= num[i]
        ans %= 998244353
print(ans%998244353)

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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    if a[0] != 0:
        return 0
    c = collections.Counter(a)
    r = 1
    for i in range(1,n):
        r *= c[a[i]-1]
        r %= mod2

    return r


print(main())

Solution (Python):
N=int(input())
D=list(map(int,input().split()))
layer=[0]*N

mx=0
for item in D:
    layer[item]+=1
    mx=max(mx,item)

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

ans=1
for i in range(mx):
    ans*=(layer[i]**layer[i+1])

print(ans%998244353)

Solution (Python):
def mod_pow(x, n, MOD):
    if n == 0:
        return 1
    if n % 2 == 0:
        result = mod_pow(x * x, n // 2, MOD)
        return result % MOD
    else:
        result = x * mod_pow(x, n - 1, MOD)
        return result % MOD

from collections import Counter        

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

count = Counter(D)
ans = 1

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

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

for i in range(max_num):

    ans *= mod_pow(count[i], count[i+1], MOD)
    ans %= MOD

print(ans)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
dic=dict()
for i in range(n):
  if d[i] not in dic:
    dic[d[i]]=1
  else:
    dic[d[i]]+=1
if d[0]!=0 or dic[0]>1:
  print(0)
else:
  l=list(dic.items())
  l.sort()
  ans=1
  for i in range(len(l)-1):
    if l[i][0]+1==l[i+1][0]:
      ans*=l[i][1]**l[i+1][1]
    else:
      print(0)
      exit()
  print(ans%998244353)

Solution (Python):
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
import scipy
from collections import deque


"""
sort, and then check how many next levels we have against the previes level
keep a prevcount, and each level is prev level ** cur elvel
"""
N = gi()
cnt = [0] * N
dist = [0] * N

MOD = 998244353

max_d = 0

for i in range(N):
    dist[i] = gi()
    cnt[dist[i]] += 1
    max_d = max(max_d, dist[i])


def run():
    if cnt[0] != 1 or dist[0] > 0:
        print(0)
        return

    ans = 1
    for i in range(1, max_d + 1):
        if cnt[i] == 0:
            print(0)
            return
        for _ in range(cnt[i]):
            ans *= cnt[i - 1]
            ans %= MOD

    print(ans)


run()

Solution (Python):
from collections import Counter
MOD = 998244353
def solve(n, d):
    if d[0] > 0:
        return 0
    d = sorted(d)  
    if (n > 1) and (d[1] == 0):
        return 0
    D = Counter(d) 
    k = sorted(list(D.keys()))
    m = len(k)  
    if m -1 != k[-1]:
        return 0
    res = 1
    for i in range(1, m):
        p = D[k[i-1]]
        q = D[k[i]]
        res = (res * (p**q)) % MOD
    return res
n = int(input())
d = list(map(int, input().split()))
print(solve(n, d))

Solution (Python):
n = int(input())
mod = 998244353
data = [0]*(n+1)
d = list(map(int,input().split()))
if d[0] != 0:
    print(0)
    exit()
for x in d:
    data[x] += 1

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

q = 0
ans = 1
for i in range(1,n):
    ans *= pow(data[i-1],data[i],mod)
    ans %= mod

print(ans)

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

Solution (Python):
from collections import Counter

def print_0():
  print(0)
  exit()

MOD = 998244353
N = int(input())
inputD = list(map(int,input().split()))
D = Counter(inputD)
if inputD[0] != 0 or D[0]!=1: print_0()

max_rank = max(D.keys())
ans = 1
for i in range(max_rank+1):
  if i not in D:
    print_0()
  elif i not in [0,1]:
    ans *= D[i-1]**D[i]
print(ans%MOD)

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

zero_flag = False
if dic_c[0]!= 1 or dd[0]!=0 :
  zero_flag = True

for i in range(max(dd)):
  if dic_c[i] == 0 :
    zero_flag = True

if zero_flag:
  print(0)  
else:
  ans = 1
  for i in range(1,max(dd)):
    ans *= dic_c[i]**dic_c[i+1]
  print(ans % 998244353)

Solution (Python):
from collections import Counter

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

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

print(ans % 998244353)

Solution (Python):
n=int(input())
d=list(map(int,input().split()))
x=max(d)
li=[0]*(x+1)
for i in range(n):
    li[d[i]]+=1
if d[0]!=0 or li[0]!=1:
    print(0) 
else:
    ans=1
    for i in range(2,len(li)):
        ans*=pow(li[i-1],li[i],998244353)   
    print(ans%998244353)

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

N = int(input())
tmp = [int(i) for i in input().split()]
if tmp[0] != 0:
    print(0)
    exit()
D = defaultdict(int)
for i in tmp:
    D[int(i)] += 1
D_len = max(tmp)

MOD = 998244353

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

answer = 1
for i in range(1,D_len+1):
    if D[i] == 0:
        answer = 0
        break
    tmp = 1
    for _ in range(D[i]):
        tmp = (tmp * D[i-1]) % MOD
    answer = (answer * tmp) % MOD

print(answer)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
M = max(A)
D = {}
for i in range(N):
  if A[i] in D:
    D[A[i]] += 1
  else:
    D[A[i]] = 1
sorted(D)
S = 1
if A[0] == 0 and D[0] == 1:
  for i in D:
    if i != M:
      if i + 1 not in D:
        S = 0
        break
      else:
        S *= (D[i] ** D[i+1]) % 998244353
        S %= 998244353
else:
  S = 0
print(S % 998244353)

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

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

a.sort()
b = set(a[1:])
if 0 in b:
    print(0)
    exit()

num = max(b) - min(b) + 1
if num != len(b):
    print(0)
    exit()

count_pre = 1
count_now = 1
ans = 1
for i in range(1, n-1):
    if a[i] == a[i+1]:
        count_now += 1
    else:
        ans *= (count_pre ** count_now)
        ans %= 998244353
        count_pre = count_now
        count_now = 1
ans *= (count_pre ** count_now)
ans %= 998244353

print(ans)

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

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

count = [0]*(max(D)+1)
ans = 1

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

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

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

MOD = 998244353

from collections import defaultdict

count = defaultdict(int)

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

for d in D:
    count[d] += 1

tmp = 0
ans = 0
check = 0
for i in range(N):
    if i == 0:
        if count[i] == 1:
            ans = 1
            tmp = 1
            check += 1
        else:
            print (0)
            exit()
    else:
        ans *= pow(tmp, count[i], MOD)
        tmp = count[i]
        check += count[i]
    
    ans %= MOD

    if check == N:
        break
    
    if count[i] == 0:
        print (0)
        exit()

print (ans % MOD)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
if a[0]!=0:print(0)
else:
  a.sort()
  mod=998244353
  if n==1:print(1)
  elif a[1]==0:print(0)
  else:
    li=[]
    ans=1
    a+=[a[-1]+1]
    check=0
    for i in range(1,n):
      if abs(a[i]-a[i+1])>=2:print(0);exit()
      if a[i]!=a[i+1]:
        if li==[]:
          li.append(i)
          check=i
        else:
          li.append(i-check)
          check=i
    for i in range(len(li)-1):
      ans=ans*(li[i]**li[i+1])
      ans%=mod
    print(ans%mod)

Solution (Python):
import cmath
import sys

INF=10**18
MOD=998244353
MAX=10**5+7

def main():
    N=int(input())
    D=list(map(int, input().split()))
    cnt=[0]*(N+1)

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

    for d in D:
        cnt[d]+=1

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

if __name__=='__main__':
    main()