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



Sample input:
4
0 1 1 2


Sample output:
2

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from math import factorial, sqrt, ceil, gcd
from functools import lru_cache, reduce
from decimal import Decimal
from operator import mul

INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)


# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 組合せnCr
def combinations_count(n, r):
    if n < r:
        return 0
    r = min(r, n - r)
    numer = reduce(mul, range(n, n - r, -1), 1)
    denom = reduce(mul, range(1, r + 1), 1)
    return numer // denom

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定
def is_prime(n):
    if n <= 1:
        return False
    p = 2
    while True:
        if p ** 2 > n:
            break
        if n % p == 0:
            return False
        p += 1
    return True


# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

# ここから書き始める
n = int(input())
d = list(map(int, input().split()))

if d[0] != 0:
    print(0)
    sys.exit()
d.sort()
cnt = [0 for i in range(n)]
for i in d:
    if i >= n:
        print(0)
        sys.exit()
    cnt[i] += 1
# print("cnt =", cnt)

ans = 1
flag = True
for i in range(n):
    if i == 0:
        if cnt[i] != 1:
            ans = 0
            break
    elif cnt[i] == 0:
        flag = False
    elif not flag:
        ans = 0
        break
    else:
        ans *= cnt[i - 1] ** cnt[i]

print(ans % 998244353)

# ans = 1
# cnt1 = 0
# cnt2 = 0
# for i in range(n):
#     if i == 0:
#         if d[i] > 0:
#             ans = 0
#             break
#         else:
#             cnt2 += 1
#     else:
#         if d[i] > d[i - 1] + 1:
#             ans = 0
#             break
#         elif d[i] == d[i - 1] + 1:
#             if cnt1 > 0:
#                 ans *= cnt1 ** cnt2
#             cnt1 = cnt2
#             cnt2 = 1
#         else:
#             cnt2 += 1
# # if cnt1 > 0:
# ans *= cnt1 ** cnt2
# print(ans)

Submission status: Accepted

Solution code:
mod = 998244353

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

if D[0] != 0:
    res = 0
else:
    dict_D = {}
    for d in D:
        if d not in dict_D:
            dict_D[d] = 0
        dict_D[d] += 1

    max_D = sorted(D)[-1]

    res = 1
    parent = 1
    for i in range(max_D+1):
        if i == 0:
            if dict_D[0] != 1:
                res = 0
                break
        else:
            if i not in dict_D:
                res = 0
                break
            else:
                if i != 1:
                    res *= (parent**dict_D[i])
                    res %= mod
                parent = dict_D[i]

print(res)

Submission status: Accepted

Solution code:
#import sys
#import numpy as np
import math
#import itertools
#from fractions import Fraction
#import itertools
from collections import deque
from collections import Counter
#import heapq
#from fractions  import gcd
#input=sys.stdin.readline
#import bisect

Mod=998244353

n=int(input())
d=list(map(int,input().split()))
c=Counter(d)
ans=1
if d[0]!=0:
  print(0)
  exit()
for i in c:
  if i==0:
    if c[i]!=1:
      print(0)
      exit()
  else:
    if i-1 not in c:
      print(0)
      exit()
    else:
      ans*=pow(c[i-1],c[i],Mod)
      ans%=Mod
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

max_D = max(D_list)
cnt_dic = dict(Counter(D_list))

if (D_list[0] == 0) & (cnt_dic.get(0) == 1):
    ans = 1
    for n in range(1, max_D + 1):
        ans *= cnt_dic.get(n - 1, 0)  ** cnt_dic.get(n, 1)
    print(ans % 998244353)
else: 
    print(0)

Submission status: Accepted

Solution code:
def print0():
    print(0)
    exit()

n = int(input())
d = list(map(int, input().split()))
mod = 998244353
ans = 1
maxd = max(d)
numcnt = [0] * (maxd + 1)
for i in d:
    numcnt[i] += 1
if not d[0] == 0 or not numcnt[0] == 1:
    print0()
for i in range(1, maxd + 1):
    if numcnt[i] == 0:
        print0()
    ans *= pow(numcnt[i - 1], numcnt[i], mod)
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 20 09:37:56 2020

@author: NEC-PCuser
"""

N = int(input())

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


if D[0] != 0:
    print(0)
else:
    n_count = [0] * 100000
    for i in range(N):
        n_count[D[i]] += 1
        
    count = 1
    ans = 1
    for j in range(1, len(n_count)):
        count += n_count[j]
        if n_count[j] == 0:
            break
        else:
            ans = (ans * (n_count[j - 1] ** n_count[j])) % 998244353
    if (count != N):
        print(0)
    else:
        print(ans)

Submission status: Accepted

Solution code:
import sys
import collections

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=998244353
 

#+++++

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al = lin()
	if al[0] != 0:
		return 0
	if al.count(0) != 1:
		return 0
	
	alc = collections.Counter(al)
	mm = max(alc.keys())
	
	b = alc.keys()
	b=list(b)
	b.sort()
	if list(range(mm+1)) != b:
		#pa('rrrr')
		return 0
		
	v = 1
	p=1
	for k in range(1, mm+1):
		num = alc[k]
		add = pow(p, num, mod)
		v *= add
		v %= mod
		p = num
	return v
		
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

Solution code:
N=int(input())
D=list(map(int,input().split()))
MOD=998244353
goal=-1
tree=[0]*(N)
ans=1
for i in range(N):
    a=D[i]
    goal=max(a,goal)
    tree[a]+=1
if tree[0]==1 and D[0]==0:
    for i in range(1,goal+1):
        ans*=pow(tree[i-1],tree[i],MOD)
        ans%=MOD
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
import sys
from collections import Counter

input = sys.stdin.readline

N = int(input())
ans = 1
mod = 998244353
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
    exit()
D = sorted(Counter(D).items())
tmp = D[0][1]
stream = 0
for n, i in D:
    if stream != n:
        print(0)
        exit()
    if n == 0 and i == 1:
        stream += 1
        continue
    elif n==0:
        print(0)
        exit()
    ans *= pow(tmp, i)
    ans %= mod
    tmp = i
    stream += 1
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
MOD = 998244353
n = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
else:
    cnt = Counter(D[1:])
    if 0 < cnt[0]:
        print(0)
    else:
        U = 10**5
        ans = 1
        prev_v = 1
        for i in range(1, U):
            ans *= pow(prev_v, cnt[i], MOD)
            ans %= MOD
            prev_v = cnt[i]
        print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AtoZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  divisors.sort()
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
A = IL()
M = max(A)
dic = DD(int)
for a in A:
  dic[a] += 1
if dic[0] != 1 or A[0] != 0:
  print(0)
  exit()
else:
  ans = 1
  for i in range(M):
    ans *= pow(dic[i],dic[i+1])
    ans %= 998244353

print(ans)

Submission status: Accepted

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

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

max_d = max(d)
cnt = [0] * (max_d + 1)

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

if cnt[0] > 1:
    print(0)
    exit(0)

ans = 1

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

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

dcnt = Counter(d)
if d[0] > 0 or dcnt[0] > 1:
    print(0)
else:
    ans = 1
    for i in dcnt:
        if i > 0:
            ans = ans * dcnt[i - 1]**dcnt[i] % mod
    print(ans)

Submission status: Accepted

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

a = [0] * n
for d_ in d:
    a[d_] += 1

res = d[0] == 0 and a[0] == 1
for i, j in zip(a, a[1:]):
    res = res * i ** j % mod

print(res)

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

from collections import Counter

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

    if D[0]!=0 or Dc[0]!=1:
        print(0)
    else:
        mx = max(D)
        mod = 998244353
        ret = 1
        for i in range(1, mx+1):
            ret *= pow(Dc.get(i-1,0), Dc.get(i,0), mod)
            ret %= mod
        print(ret)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

a = [0] * n
for d_ in d:
    a[d_] += 1

res = d[0] == 0 and a[0] == 1
pre = 1
for a_ in a:
    res = res * pre ** a_ % mod
    pre = a_

print(res)

Submission status: Accepted

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

a = [0] * n
for d_ in d:
    a[d_] += 1

res = d[0] == 0 and a[0] == 1
pre = 1
for a_ in a:
    res *= pre ** a_
    res %= mod
    pre = a_

print(res)

Submission status: Accepted

Solution code:
from collections import Counter

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

a = sorted(list(set(d)))

if d[0] != 0:
    ans = 0
elif a[-1] != len(a)-1:
    ans = 0
else:
    c = sorted(Counter(d).items())
    if c[0] != (0, 1):
        ans = 0
    else:
        ans = 1
        pre = 1
        for k, v in c:
            ans *= pre ** v
            ans %= mod
            pre = v

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


MOD = 998244353
N = int(input())
D = list(map(int, input().split()))
cnt_D = Counter(D)
if D[0] != 0 or cnt_D.get(0, 0) != 1:
    print(0)
else:
    prev = 1
    ans = 1
    for i in range(1, max(D) + 1):
        v = cnt_D.get(i, 0)
        ans *= pow(prev, v, MOD)
        ans %= MOD
        prev = v
    print(ans)

Submission status: Accepted

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

c = Counter(d)
max_d = max(d)#最大の深さを求める
ans = 1
previous_leaf = 1

#頂点1から頂点1の距離が0以外だったらansを0にする
if d[0] != 0:
    ans = 0
#距離0のノードは頂点1のみの1個でなければ不可能
elif c[0] != 1:
    ans = 0
#前の距離のノードa個あったら場所でa通りあり、さらに前の距離のノード1個につき今の距離のノードb個の子がいるので、全部でa**b通り増えてく
else:
    for i in range(1,max_d+1):
        ans *= (previous_leaf**c[i])
        ans %= 998244353

        #前の距離のノード
        previous_leaf = c[i]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right

def main():
    N = int(input())
    D = list(map(int, input().split()))
    p = 998244353
    if D[0] > 0:
        exit(print(0))
    l = Counter(D)
    if l[0] > 1:
        exit(print(0))
    ans = 1
    for i in range(1,max(l)+1):
        if l[i] == 0:
            exit(print(0))
        ans *= pow(l[i-1],l[i],p)
        ans %= p
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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, max(c)+1):
    if not c[i]:
        print(0)
        exit()
    
    ans *= pow(c[i-1], c[i], MOD) % MOD

print(ans%MOD)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline

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

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

from collections import Counter
counter = Counter(D)

for k,v in counter.items():
  counts[k] = v

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

print(ans)

Submission status: Accepted

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

if d[0]!=0:
  print(0)
  exit(0)
  
m=max(d)
counts=[0]*(m+1)

import collections
counter=collections.Counter(d)

for key,val in counter.items():
  counts[key]=val
if counts[0]!=1:
  print(0)
  exit(0)
ans=1
div=998244353
for i in range(1,len(counts)):
  ans*=pow(counts[i-1],counts[i],div)
  ans%=div
  
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
dl = list(map(int,input().split()))
if dl[0] != 0:
  print(0)
  exit()
d_dic = dict()
for d in dl:
  if d in d_dic:
    d_dic[d] += 1
  else:
    d_dic[d] = 1
l = max(d_dic.keys())
for i in range(l):
  if not i in d_dic:
    print(0)
    exit()
if d_dic[0] != 1:
  print(0)
  exit()
ans = 1
for i in range(l):
  ans *= d_dic[i]**d_dic[i+1]
  ans = ans%998244353
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import Counter
N = int(sys.stdin.readline().rstrip())
D = list(map(int, sys.stdin.readline().rstrip().split()))
mod = 998244353

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

d_cnt = sorted(Counter(D).items())

ans = 1
par = 1
prev = 0
for k, v in d_cnt:
    if k > 0:
        if prev + 1 == k:
            ans *= pow(par, v, mod)
            ans %= mod
            par = v
            prev = k
        else:
            print(0)
            exit()
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
N=int(input())
D=list(map(int, input().split() ))
d=dict(Counter(D))
p=998244353
#print(d)
maxi=max(d)

ans=1
if D[0]!=0:
  print(0)
else:
  if d[0]!=1:
    print(0)
  else:
    for i in range(1,maxi+1):
      if i not in d:
        print(0)
        exit()
      else:
        s =  (d[i-1]**d[i])%p
        ans = (ans * s)%p
    print(ans)

Submission status: Accepted

Solution code:
mod = 998244353


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

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

CNT = [0] * n
for d in D:
    CNT[d] += 1

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)
from math import floor,ceil,sqrt,factorial,log
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 
#整数input
def ii(): return int(sys.stdin.readline().rstrip()) #int(input())
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) #list(map(int,input().split()))
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]
#文字列input
def ss(): return sys.stdin.readline().rstrip() #input()
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) #list(input().split())
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]

#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
#本当に貪欲法か？ DP法では？？
mod=998244353
n=ii()
arr=limii()

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

if n==1 and arr[0]==0:
    print(1)
    exit()


chk=True

p=Counter(arr)

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

tmp=0
for i in range(1,max(list(p))+1):
    if p[i]>0:
        pass
    else:
        print(0)
        exit()

ans=1

#掛け算の場合
# (a*b)%mod と同等
def mul(a, b):
    return ((a % mod) * (b % mod)) % mod

#割り算の場合
# (a//b)%mod と同等
def div(a,b):
    return a*pow(b, mod-2, mod)

#累乗の場合
#a**n %p と同等
def p_f(a,n,pp):
    bi=str(format(n,"b"))#2進表現に
    res=1
    for i in range(len(bi)):
        res=(res*res) %pp
        if bi[i]=="1":
            res=(res*a) %pp
    return res

for i in range(max(p.keys())):
    
    ans=(ans*pow(p[i], p[i+1], mod)) %mod
    
print(ans%mod)

Submission status: Accepted

Solution code:
N=int(input())
import collections
l=list(map(int,input().split()))
cnt=collections.Counter(l)
if cnt[0] > 1 or l[0] != 0:
   print(0)
   exit()
ans=1
mod=998244353
for i in range(1,max(cnt)):
   ans*=pow(cnt[i],cnt[i+1],mod)
   ans%=mod
print(ans)

Submission status: Accepted

Solution code:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
flag = False
n = I()
D = readInts()
dic = defaultdict(int)
for i in range(n):
    dic[D[i]] += 1
# 後ろから見る
for i in range(n-1,0,-1):
    if dic[i] != 0:
        flag = True
    if flag and dic[i] == 0:
        print(0)
        exit()
if dic[0] != 1: # 距離が0(頂点が1)が絶対に存在しないと駄目
    print(0)
    exit()
if D[0] != 0: # 頂点1が絶対に存在 距離が 0
    print(0)
    exit()
ans = 1
#print(dic)
for i in range(1,n):
    ans *= pow(dic[i-1],dic[i],mod)
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
mod=998244353
b=[0]*n
for i in a:
    b[i]+=1
ans=0
if a[0]==0 and 1==b[0]:
    ans=1
for i,j in zip(b,b[1:]):
    ans=ans*i**j%mod
print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
mod=998244353
d={}
for i in a:
  d[i]=d.get(i,0)+1
b=list(d.items())
b.sort()
ans=1
if a[0]!=0 or len(b)!=b[-1][0]+1 or b[0][1]!=1:
    ans=0
for i in range(1,len(b)):
    ans*=b[i-1][1]**b[i][1]
    ans%=mod
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
if a[0]:
    print(0)
    exit()
a.sort()
j=0
l=list()
#print(a)
for i in range(max(a)+1):
    k=0
    while j<n and a[j]==i:
        j+=1
        k+=1
        #print(l,j,k)
    l.append(k)
#print(l)
p=l[0]==1
mod=998244353
for i in range(len(l)-1):
    #print(p)
    p=(p*pow(l[i],l[i+1],mod))%mod
print(p%mod)

Submission status: Accepted

Solution code:
from collections import Counter

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import*
n, *d= map(int, open(0).read().split())
c = Counter(d)
if d[0] or c[0] > 1:
    exit(print(0))
MOD = 998244353
m = 1
for i in range(1, max(d)):
    m *= pow(c[i], c[i + 1], MOD)
    m %= MOD
print(m)

Submission status: Accepted

Solution code:
def count(l):
    res = {}
    for d in l:
        if d in res:
            res[d] += 1
        else:
            res[d] = 1
    return res

n = int(input())
data = list(map(int, input().split()))
if data[0] != 0:
    print(0)
    exit()
data = count(data)
mod = 998244353
if data[0] != 1:
    print(0)
    exit()
ans = 1
for depth, edges in data.items():
    if depth == 0:
        continue
    if depth - 1 not in data:
        print(0)
        exit()
    ans *= data[depth-1]**edges
    ans %= mod
print(ans%mod)

Submission status: Accepted

Solution code:
from collections import Counter
mod = 998244353
num_nodes = int(input())
d_ls = list(map(int, input().split()))
d_max = max(d_ls)
c = Counter(d_ls)

if c[0] == 1 and d_ls[0] == 0 and c[1] > 0:
    ans = 1
else:
    ans = 0

for i in range(1,d_max):
    ans *= c[i] ** c[i+1]
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import collections
mod = 998244353

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

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

Dep = collections.Counter(D)
values, counts = zip(*Dep.most_common())
#print(values)
#print(counts)
#print(Dep)
ans = 1
for i in range(1,len(values)):
    if Dep[i-1] == 0:
        ans = 0
    ans *= pow(Dep[i-1],Dep[i], mod)
    #print(i,Dep[i])
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for i in range(N)]
def dp2(ini, i, j): return [[ini]*i for i2 in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for i2 in range(j)] for i3 in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from collections import Counter # a = Counter(A).most_common()
#from itertools import accumulate #list(accumulate(A))

N = ii()
A = li()
max_num = 10**5+1
num_list = [0] * (max_num)

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

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

if num_list[0] > 1:
    print(0)
    exit()

ans = 1
for i in range(2, max_num):
    if num_list[i] != 0:
        ans *= pow(num_list[i-1], num_list[i], 998244353)

else:
    print(ans%998244353)

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int, input().split()))
DL = [0] * N
mod = 998244353
if D[0] != 0:
    print(0)
    exit()

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

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

ans = 1
for i in range(1, N):
    if DL[i] == 0:
        if sum(DL[i:]) != 0:
            print(0)
            exit()
        else:
            print(ans%mod)
            exit()

    ans *= pow(DL[i-1], DL[i], mod)
    ans %= mod
print(ans % mod)

Submission status: Accepted

Solution code:
mod = 998244353
n = int(input())
d = list(map(int, input().split()))
if d[0] != 0:
    print(0)
    exit()
lis = [0] * n
for i in d:
    lis[i] += 1
if lis[0] != 1:
    print(0)
    exit()
ans = 1
for i in range(1, n):
    ans *= pow(lis[i-1], lis[i], mod)
    ans %= mod
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter


def solve(n, arr):
    if arr[0] != 0:
        return 0

    c = Counter(arr)

    if c[0] != 1:
        return 0

    for i in range(max(c) + 1):
        if i not in c:
            return 0

    c = list(c.items())
    c.sort()

    prev = 1
    ans = 1
    mod = 998244353

    for _, i in c[1:]:
        ans *= prev**i % mod
        ans %= mod
        prev = i

    return ans


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

print(solve(n, arr))

Submission status: Accepted

Solution code:
from collections import Counter


def solve(n, arr):
    if arr[0] != 0:
        return 0

    c = Counter(arr)

    if c[0] != 1:
        return 0

    for i in range(max(c) + 1):
        if i not in c:
            return 0

    c = list(c.items())
    c.sort()

    prev = 1
    ans = 1
    mod = 998244353

    for _, i in c[1:]:
        ans *= pow(prev, i) % mod
        ans %= mod
        prev = i

    return ans


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

print(solve(n, arr))