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: Runtime Error

Solution code:
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
    print(0)
C = Counter(D)
ans = 1
for i in range(1, max(D)+1):
    if C[i] == 0:
        ans = 0
        break
    else:
        ans *= pow(C[i-1], C[i], MOD)
        ans %= MOD
print(ans)

Submission status: Runtime Error

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

setD = list(set(d))
setD.sort()
ans = 1
nodeMulti = 1

if d[0] > 0:
        print(0)
        exit()
else:
        #グラフ作れるかチェック
        for i in range(len(setD)-1):
                c[setD[i]]
        c = Counter(d)
        for s in setD:
                ans *= math.factorial(c[s])
                nodeMulti *= c[s]
        nodeMulti //= setD[-1]
        # ans *= nodeMulti
        print(ans%998244353)

Submission status: Runtime Error

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

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

waru = 998244353

dic = {}
dic2 = {}

for i in range(1, N+1):
    dic[i] = D[i-1]

for number, distance in dic.items():
    if distance in dic2:
        dic2[distance] = dic2[distance] + 1
    else:
        dic2[distance] = 1

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

l = []

for i in range(1, len(dic2.items()) -1):
    l.append(dic2[i] ** dic2[i+1])

r = 1
for x in l:
    r = r * x

if r < waru:
    print(r)
else:
    #d, m = divmod(r, waru)
    print(r)

Submission status: Runtime Error

Solution code:
import math
n=int(input())
d=list(map(int,input().split()))
e=set(d)
f=max(d)
ans=0
ans1=1
c=True
count=[]
for i in range(f+1):
    if i not in e:
        c=False
if d[0]!=0:
    print(ans)
elif c==False:
    print(ans)
else:
    for j in range(n):
        count.append(d.count(j))
    k=1
    while count[k]>0:
        if k==1:
            ans1*=count[k]
        elif k==f:
            ans1=math.factorial(ans1)//math.factorial(ans1-count[k])
        else:
            ans1*=(count[k]+1)*math.factorial(count[k])//(math.factorial(count[k]-count[k-1])*math.factorial(count[k-1]))
        #print(ans1)
        k+=1
print(ans1%998244353)
#print(count)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
D = list(map(int,input().split()))
defi = 998244353

ans = 1
for i in range(max(D)+1):
  if D.count(i) == 0 or D.count(0) >= 2:
    ans = 0
    break
  elif max(D) == 0:
    break
  else:
    if i >= 1:
      ans *= math.pow(D.count(i-1),D.count(i))
      
print(int(ans % defi))

Submission status: Runtime Error

Solution code:
def main():
    MOD = 998244353
    N = int(input())
    D = [int(i) for i in input().split()]
    if D[0] != 0:
        return print(0)
    from collections import Counter
    from math import factorial
    c = Counter(D)
    ans = 1
    for k, v in c.items():
        if k == 0 or k == 1:
            continue
        elif v == 0:
            return print(0)
        else:
            n = v + c[k-1] - 1
            r = c[k-1] - 1
            ans *= factorial(n) / factorial(r) / factorial(n - r)
            ans = int(ans) % MOD
            if c[k-1] > v:
                ans *= (c[k-1] - v)
            ans = int(ans) % MOD
    print(ans % MOD)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import numpy as np


def q1(n):
    return (n - 1) // 2


def q2(n, D):
    MAXINT = 998244353
    counts = np.sum(D == np.arange(n)[:, None], axis=1)
    idx = np.where(counts == 0)[0]
    minidx = np.min(idx)
    if len(idx) == n - minidx and (idx == np.arange(minidx, n)).all():
        return int(np.prod([counts[i] ** counts[i + 1] for i in range(minidx)])) % MAXINT
    return 0


if __name__ == '__main__':

    # A 問題
    # n = int(input())
    # print(q1(n))

    # B 問題
    n = int(input())
    D = np.asarray(list(map(int, input().split())))
    # n = 4
    # D = np.array([0, 1, 1, 2])
    # D = np.array([1, 1, 1, 1])
    # n = 7
    # D = np.array([0, 3, 2, 1, 2, 2, 1])
    print(q2(n, D))

Submission status: Runtime Error

Solution code:
import numpy as np

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

if not D[0] == 0:
    print("0")
elif D.count(0) >= 2:
    print("0")
else:
    D.sort()
    ans = [D.index(i) for i in range(1,max(D)+1)]
    sabun = [ans[i] - ans[i-1] for i in range(1,len(ans))]
    sabun.append(N-ans[-1])
    ans = [pow(i+1,sabun[i]) for i in range(len(sabun))]
    ans = np.prod(ans, axis = 0)
    print(ans%998244353)

Submission status: Runtime Error

Solution code:
#inputは文字列として取得
input1=input()
input2=input()
#splitする必要がある
input2=input2.split(" ")

N=int(input1)
D=[int(s) for s in input2]

dp=[[0] * max(D) for i in range(max(D))]

def pow(a,b):
    
    if dp[a-1][b-1]!=0:
        return dp[a-1][b-1]

    else:
        ans=a**b
        dp[a-1][b-1]=ans
        return ans

ans=None

if D.count(0)==1 and D[0]==0:

    mul=1
    n=1

    for i in range(1,max(D)+1):
        m=D.count(i)
        mul*=pow(n,m)
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))

Submission status: Runtime Error

Solution code:
N = int(input())
D = list(map(int, input().split()))
ans=1
if not D[0]==0:
  ans=0
else:
  for i in D[1,N]:
    ans = ans*i
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
 
output = 1
if a[0] != 0:
    print(0)
elif max(a) == 1:
    print(1)
else:
    for i in range(2, max(a)+1):
        if a.count(i) != 0
            output *= pow(a.count(i-1), a.count(i), 998244353)
    print(output)

Submission status: Runtime Error

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

if l[0] != 0:
  print(0)
  exit()
del l[0]
if l.count(0) != 0:
  print(0)
  exit()
l.remove(0)
for i in range(max(l)):
  if l.count(i+1) == 0:
    print(0)
    exit()
  else:
    if i == 0:
      a = l.count(i+1)
    else:
      a *= l.count(i+1)
ans = a % 998244353
print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
list = list(map(int, input().split()))
ans = 1

dict = {}
for i in range(N):
    if list[i] in dict.keys():
        dict[list[i]] += 1
    else:
        dict.setdefault(list[i], 1)

max = max(dict)

try:
    for i in range(1,max+1):
        ans = (ans * (dict[i-1] ** dict[i])) % 998244353
except KeyError:
    ans = 0

if dict[0] != 1:
    ans = 0

print(ans)

Submission status: Runtime Error

Solution code:
def ContinueCnt(A):
    datas = [_ for _ in range(max(A) + 1)]
    counts = [0] * len(datas)
    if set(datas) != set(A):
        print(0)
        exit()
    for j in range(max(A) + 1):
        counts[i] = A.count(i)
    return datas, counts

def power_func(a, n, p):
    bi = str(format(n, "b"))  # 2進表現に
    res = 1
    for i in range(len(bi)):
        res = (res * res) % p
        if bi[i] == "1":
            res = (res * a) % p
    return res

INF = 998244353

N = int(input())
D = list(map(int, input().split()))
D_datas, D_counts = ContinueCnt(D)
# print(D_datas, D_counts)  # for debug

# first check zero
if D_counts[0] != 1:
    print(0)
    exit()

ans = 1
# check 1 ~ max(D)
for i in range(1, max(D_datas) + 1):
    ans *= power_func(D_counts[i - 1], D_counts[i], INF)
    # print("now = ", ans)  # for debug

print(ans % INF)

Submission status: Runtime Error

Solution code:
import numpy as np


def q1(n):
    return (n - 1) // 2


def q2(n, D):
    MAXINT = 998244353
    counts = np.sum(D == np.arange(n)[:, None], axis=1)
    idx = np.where(counts == 0)[0]
    minidx = np.min(idx)
    if len(idx) == n - minidx and idx == np.arange(minidx, n):
        return int(np.prod([counts[i] ** counts[i + 1] for i in range(minidx)])) % MAXINT
    return 0


if __name__ == '__main__':

    # A 問題
    # n = int(input())
    # print(q1(n))

    # B 問題
    n = int(input())
    D = np.asarray(list(map(int, input().split())))
    # n = 4
    # D = np.array([0, 1, 1, 2])
    # D = np.array([1, 1, 1, 1])
    # n = 7
    # D = np.array([0, 3, 2, 1, 2, 2, 1])
    print(q2(n, D))

Submission status: Runtime Error

Solution code:
n = int(input())
d = [int(i) for i in input().split()]
d.sort()
k = [0]*10005
for i in d:
    k[i] += 1
c = 1
for i in range(d[-1]):
    for j in range(k[i+1]):
        c *= k[i]
        c %= 998244353
if (k[0] >= 2):
    print(0)
else:
    print(c)

Submission status: Runtime Error

Solution code:
import numpy as np


def q1(n):
    return (n - 1) // 2


def q2(n, D):
    MAXINT = 998244353
    counts = np.sum(D == np.arange(n)[:, None], axis=1)
    idx = np.where(counts == 0)[0]
    minidx = np.min(idx)
    if len(idx) != n - minidx or idx != np.arange(minidx, n):
        return 0
    return int(np.prod([counts[i] ** counts[i + 1] for i in range(minidx)])) % MAXINT


if __name__ == '__main__':

    # A 問題
    # n = int(input())
    # print(q1(n))

    # B 問題
    n = int(input())
    D = np.asarray(list(map(int, input().split())))
    # n = 4
    # D = np.array([0, 1, 1, 2])
    # D = np.array([1, 1, 1, 1])
    # n = 7
    # D = np.array([0, 3, 2, 1, 2, 2, 1])
    print(q2(n, D))

Submission status: Runtime Error

Solution code:
N=int(input())
D=list(map(int,input()))
ans=1
if not D[0]==0:
  ans=0
else:
  for i in range(1,N):
    ans = ans*D[i]
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np


def q1(n):
    return (n - 1) // 2


def q2(n, D):
    MAXINT = 998244353
    counts = np.sum(D == np.arange(n)[:, None], axis=1)
    idx = np.where(counts == 0)[0]
    minidx = np.min(idx)
    if len(idx) != n - minidx and idx == np.arange(minidx, n):
        return 0
    return int(np.prod([counts[i] ** counts[i + 1] for i in range(minidx)])) % MAXINT


if __name__ == '__main__':

    # A 問題
    # n = int(input())
    # print(q1(n))

    # B 問題
    n = int(input())
    D = np.asarray(list(map(int, input().split())))
    # n = 4
    # D = np.array([0, 1, 1, 2])
    # D = np.array([1, 1, 1, 1])
    # n = 7
    # D = np.array([0, 3, 2, 1, 2, 2, 1])
    print(q2(n, D))

Submission status: Runtime Error

Solution code:
import heapq

n = int(input())
d = list(map(int, input().split()))
heapq.heapify(d)
if d[0] != 0:
    print(0)
else:
    pre = -1
    num = 0
    ans = 1
    for i in range(len(d)):
        if d[0] == 0:
            if i > 0:
                print(0)
                break
            else:
                pre = 0
                heapq.heappop(d)
                continue
        if d[0] == pre:
            num += 1
            if i == n-1:
                ans *= d[i]**num
            heapq.heappop(d)
        elif d[0] - pre > 1:
            print(0)
            break
        else:
            if i == 1:
                num = 1
                pre = d[0]
                heapq.heappop(d)
                continue
            ans *= pre**num
            pre = d[0]
            if i == n-1:
                ans *= pre
            num = 1
            heapq.heappop(d)
    else:
        print(ans)

Submission status: Runtime Error

Solution code:

N = int(input().strip())
array = list(map(int, input().strip().split(' ')))
if array[0] != 0:
    count = 0
if array[]
else:
    for i in range(N//2):
        if array[i] != 0:
            count = count * array[i]
        if array[N-i-1] !=0:
            count  = count * array[N-i]
    if N % 2 !=0:
        if array[(N//2 + 1)] != 0:
            count = count / array[(N//2 + 1)]
            
ans = count % 998244353 
print(ans)

Submission status: Runtime Error

Solution code:
LARGE = 998244353


def solve(n, d_list):
    cnt = [0] * (max(d_list) + 1)
    for i in range(n):
        cnt[d_list[i]] += 1
    if cnt[0] != 1 or d_list[0] != 0:
        return 0
    res = 1
    for i in range(max(d_list)):
        res *= pow(cnt[i], cnt[i + 1], LARGE)
        res %= LARGE
    return res


def main():
    n = int(input())
    d_list = list(map(int(input())))
    res = solve(n, d_list)
    print(res)


def test():
    assert solve(4, [0, 1, 1, 2]) == 2
    assert solve(4, [1, 1, 1, 1]) == 0
    assert solve(7, [0, 3, 2, 1, 2, 2, 1]) == 24


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

Submission status: Runtime Error

Solution code:
import numpy as np


def q1(n):
    return (n - 1) // 2


def q2(n, D):
    MAXINT = 998244353
    counts = np.sum(D == np.arange(n)[:, None], axis=1)
    idx = np.where(counts == 0)[0]
    minidx = np.min(idx)
    if len(idx) != n - minidx:
        return 0
    return int(np.prod([counts[i] ** counts[i + 1] for i in range(minidx)])) % MAXINT


if __name__ == '__main__':

    # A 問題
    # n = int(input())
    # print(q1(n))

    # B 問題
    n = int(input())
    D = np.asarray(list(map(int, input().split())))
    # n = 4
    # D = np.array([0, 1, 1, 2])
    # D = np.array([1, 1, 1, 1])
    # n = 7
    # D = np.array([0, 3, 2, 1, 2, 2, 1])
    print(q2(n, D))

Submission status: Runtime Error

Solution code:
n = int(input())
d = [int(i) for i in input().split()]
d.sort()
k = [0]*10005
for i in d:
    k[i] += 1
c = 1
for i in range(d[-1]):
    c *= k[i]**k[i+1]
    c %= 998244353
if (k[0] >= 2):
    print(0)
else:
    print(c)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
D = list(map(int,input().split()))
defi = 998244353

ans = 1
for i in range(max(D)+1):
  if D.count(i) == 0 or D.count(0) >= 2:
    ans = 0
    break
  else:
    if i >= 1:
      ans *= math.pow(D.count(i-1),D.count(i))
      
print(int(ans % defi))

Submission status: Runtime Error

Solution code:
import collections


def mod_pow(a, n, mod):
    result = 1
    a_n = a
    while n > 0:
        if n & 1:
            result = result * a_n % mod
        a_n = a_n * a_n % mod
        n >>= 1
    return result


def main(n, d):
    MOD = 998244353
    counter = collections.Counter(d)
    if counter[0] != 1:
        return 0
    if 0 in counter.values:
        return 0
    pows = [mod_pow(a=counter[i - 1], n=counter[i], mod=MOD) for i in
            range(1, max(counter.keys()) + 1)]
    patterns = 1
    for p in pows:
        patterns *= p
        patterns %= MOD
    return patterns


if __name__ == '__main__':
    N = int(input())
    D = list(map(int, input().split()))
    print(main(n=N, d=D))

Submission status: Runtime Error

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

if l[0] != 0:
  print(0)
  exit()
del l[0]
if l.count(0) != 0:
  print(0)
  exit()
l.remove(0)
for i in range(max(l)):
  if l.count(i+1) == 0:
    print(0)
    exit()
  else:
    if i == 0:
      a = l.count(i+1)
    else:
      a *= l.count(i+1)
print(a

Submission status: Runtime Error

Solution code:

N = int(input())

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

if D[0] != 0 or D.count(0) !=1:
    print("0")
    exit()

D.sort()
#D = D[1:]
m = max(D)
counted = []

for i in range(1,m):
    c = D.count(i)
    if c != 0:
        counted.append(c)

#calc
result = 1
x = counted[0]
for i in range(1,len(counted)):
    y = counted[i]
    result = result * pow(x,y)
    x = y
print(result%998244353)

Submission status: Runtime Error

Solution code:
N = int(input())
ls = list(map(int,input().split()))

boxes = [[] for i in range(N)]
impossible = False
for idx,num in enumerate(ls):
    if num>=N:
        impossible=True
        break
    boxes[num].append(idx)

check_start=-1
ans = 1

if impossible:
    ans = 0
elif boxes[0][0]!=0:
    ans = 0
elif len(boxes[0])>=2:
    ans = 0
else:
    for i in range(N-1):
        a = len(boxes[i])
        b = len(boxes[i+1])
        if a == 0:
            check_start = i
            rest_ls = boxes[i:]
            if rest_ls.count([])!=len(rest_ls):
                ans = 0
            break
        ans*=a**b
baisu = ans//998244353
print(ans-(998244353*baisu))

Submission status: Runtime Error

Solution code:
import collections


def mod_pow(a, n, mod):
    result = 1
    a_n = a
    while n > 0:
        if n & 1:
            result = result * a_n % mod
        a_n = a_n * a_n % mod
        n >>= 1
    return result


def main(n, d):
    MOD = 998244353
    counter = collections.Counter(d)
    pows = [mod_pow(a=counter[i - 1], n=counter[i], mod=MOD) for i in
            max(counter.keys()) + 1]
    patterns = 1
    for p in pows:
        patterns *= p
        patterns %= MOD
    return patterns


if __name__ == '__main__':
    N = int(input())
    D = list(map(int, input().split()))
    print(main(n=N, d=D))

Submission status: Runtime Error

Solution code:
import math
N = int(input())
D = [int(x) for x in input().split()]

num = [0] * 10000

maxd = 0
for d in D:
    num[d] += 1
    maxd = max(d, maxd)

if D[0] != 0 or num[0] != 1:
    ans = 0
else:
    ans = 1
    for i in range(1, maxd):
        for _ in range(num[i+1]):
            ans *= num[i]%998244353
        ans %= 998244353
        if ans == 0:
            break
print(ans%998244353)

Submission status: Runtime Error

Solution code:
N=int(input())
D=list(map(int,input()))
ans=1
if not D[0]==0:
  ans=0
else:
  for i in D[1,N]:
    ans = ans*i
print(ans)

Submission status: Runtime Error

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

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

waru = 998244353

dic = {}
dic2 = {}

for i in range(1, N+1):
    dic[i] = D[i-1]

for number, distance in dic.items():
    if distance in dic2:
        dic2[distance] = dic2[distance] + 1
    else:
        dic2[distance] = 1

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

l = []

for i in range(1, len(dic2.items()) -1):
    l.append(dic2[i] ** dic2[i+1])

r = 1
for x in l:
    r *= x
d, m = divmod(r, waru)
print(m)

Submission status: Runtime Error

Solution code:
import collections
import sys
sys.setrecursionlimit(100000*2)
nCr = {}


def cmb(n, r):
    if r == 0 or r == n:
        return 1
    if r == 1:
        return n
    if (n, r) in nCr:
        return nCr[(n, r)]
    nCr[(n, r)] = cmb(n-1, r) + cmb(n-1, r-1)
    return nCr[(n, r)]


def main():
    n = int(input())
    d_list = list(map(int, input().split()))

    # d_list.sort()
    d_max = max(d_list)
    d_col = collections.Counter(d_list)

    memo = {}
    ans = 1
    for i in range(d_max, 0, -1):
        d_col_i = d_col.get(i, 0)
        temp_cmb = 0
        if d_col_i in memo:
            temp_cmb = memo[temp_cmb]
        else:
            for j in range(d_col_i, (d_col_i // 2), -1):
                temp = cmb(d_col_i, j)
                temp_cmb += temp
            memo[d_col_i] = temp_cmb
        temp_ans = d_col.get(i - 1, 0) * temp_cmb
        ans = (ans * temp_ans) % 998244353
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

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

ans = -1

d_count = [0] * (max(d)+1)
for i in range(n):
  d_count[d[i]] += 1

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

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

ans = ans % 998244353

print(int(ans))

Submission status: Runtime Error

Solution code:
# ABC140E
class SegTree:
    def __init__(self, init_val, n, ide_ele, seg_func):
        self.segfunc = seg_func
        self.num = 2**(n-1).bit_length()
        self.ide_ele = ide_ele
        self.seg=[self.ide_ele]*2*self.num
        for i in range(n):
            self.seg[i+self.num-1]=init_val[i]    
        for i in range(self.num-2,-1,-1) :
            self.seg[i]=self.segfunc(self.seg[2*i+1],self.seg[2*i+2]) 
    def update(self, k, x):
        k += self.num-1
        self.seg[k] = x
        while k+1:
            k = (k-1)//2
            self.seg[k] = self.segfunc(self.seg[k*2+1],self.seg[k*2+2])
        
    def query(self, p, q):
        if q<=p:
            return self.ide_ele
        p += self.num-1
        q += self.num-2
        res=self.ide_ele
        while q-p>1:
            if p&1 == 0:
                res = self.segfunc(res,self.seg[p])
            if q&1 == 1:
                res = self.segfunc(res,self.seg[q])
                q -= 1
            p = p//2
            q = (q-1)//2
        if p == q:
            res = self.segfunc(res,self.seg[p])
        else:
            res = self.segfunc(self.segfunc(res,self.seg[p]),self.seg[q])
        return res

n,m=map(int, input().split())
xs = []
for _ in range(m):
    L, R, C = map(int, input().split())
    xs.append((L-1, R-1, C))
xs.sort()

mx = 10**15
ss = SegTree([0] + [mx] * n, n+1, mx, min)
for l, r, c in xs:
    mm = ss.query(l, n+1)
    nx = min(mm + c, ss.query(r, r+1))
    ss.update(r, nx)
r = ss.query(n-1,n)
print(r if r != mx else -1)

Submission status: Runtime Error

Solution code:
n = int(input())
li = list(map(int,input().split()))
 
ans = 1
ans_li = [0 for i in range(max(li))]
count = 0
 
for i in range(n):
    ans_li[li[i]] += 1
 
if not ans_li[0] == 1:
    print(0)
    exit()

for i in range(1,n-1):
    ans *= ans_li[i] ** ans_li[i+1]
 
for i in range(n-1):
    if ans_li[i] == 0 and (not ans_li[i+1] == 0):
        print(0)
        exit()
 
print(ans % 998244353)

Submission status: Runtime Error

Solution code:
#b
n=int(input())
d=[int(i) for i  in input().split()]

import numpy 
c = collections.Counter(d)

m=998244353
ans=1
if d[0]!=0 or len(set(d))!=max(d)+1 or 0 in d[1:]:
    print(0)
else:
    for i in range(1,len(c)):
        ans = ams* i**c[i] %m
    print(ans)

Submission status: Runtime Error

Solution code:
import collections
import sys
import itertools
from tqdm import tqdm
from pprint import pprint

def read_a_integer():
    return int(sys.stdin.readline())

def read_a_list(dtype):
    return list(map(dtype, sys.stdin.readline().split()))

def brute_force(N, D):
    def get_distance(G):
        queue = collections.deque()
        queue.append(1)
        distance = [-1] * N
        distance[0] = 0

        while queue:
            u = queue.popleft()
            for v in G[u]:
                if distance[v-1] == -1:
                    distance[v-1] = distance[u-1] + 1
                    queue.append(v)
        return distance

    ans = 0
    all_edges = [(u, v) for u in range(1, N+1) for v in range(1+u, N+1)]
    for graph in tqdm(itertools.combinations(all_edges, N - 1)):
        G = collections.defaultdict(list)
        # print(graph)
        # graph = [(1,4),(1,7),(3,4),(4,5),(4,6),(2,3)]
        for u, v in graph:
            G[u].append(v)
            G[v].append(u)
        # print(G)
        # exit(0)
        distance = get_distance(G)
        # print(distance)
        if distance == D:
            ans += 1
            print("-" * 50)
            pprint(G)
            print("-" * 50)
        # break
    print(ans)


def main():
    N = read_a_integer()
    D = read_a_list(int)
    if D[0] != 0:
        print(0)
        return
    counter = collections.Counter(D)
    if counter[0] != 1:
        print(0)
        return
    max_D = max(D)
    ans = 1
    for i in range(2, max_D + 1):
        if i not in counter:
            print(0)
            return
        ans = (ans * (counter[i-1] ** counter[i])) % 998244353
    print(ans)
    # brute_force(N, D)

        


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import math

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

print(len(d))
print(d[n-1])

ans = -1

d_count = [0] * (max(d)+1)
for i in range(n):
  d_count[d[i]] += 1

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

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

ans = ans % 998244353

print(int(ans))

Submission status: Runtime Error

Solution code:
n=int(input())
d=[int(x) for x in input().split()]

d.sort()
dd=[0]*10**5
for i in d:
    d[i]+=1

ans=True
now=1
if(dd[0]!=1):
    ans=False
else:
    before=1

for i in dd[1:]:
    if(i==0):
        ans=False
    now=now*(before**i) % 998244353

if(ans):
    print(now)
else:
    print("0")

Submission status: Runtime Error

Solution code:
if __name__ == "__main__":
    mod = 998244353

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

    if d[0] > 0:
        print('0')
        exit()

    if len(d) == 1:
        print('1')
        exit()

    d.sort()
    d.pop(0)
    if d[0] == 0:
        print('0')
        exit()

    tmp = 1
    ans = 1
    max_num = max(d)
    dic = dict()
    for i in range(n-1):
        if d[i] in dic:
            dic[d[i]] += 1
        else:
            dic[d[i]] = 1

    for i in range(1, max_num+1):
        count = dic[i]
        if count == 0:
            print('0')
            exit()
        ans *= tmp ** count
        ans %= mod
        tmp = count

    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
L = [int(i) for i in input().split()]
LL = [0 for i in range(pow(10,5)+1)]
LLL = np.ones(pow(10,5)+1, dtype = "uint64")
 
for i in range(n):
  LL[L[i]] += 1
  
out = 1
for i in range(len(LL)-1):
  LLL[i+1] = pow(i+1, LL[i+1])

out = int(int(np.prod(LLL)) % 998244353)
if L[0] != 0 or L.count(0) != 1:
  out = 0
print(out)

Submission status: Runtime Error

Solution code:
import math
N = int(input())
D = list(map(int,input().split()))
defi = 998244353

ans = 1
for i in range(max(D)+1):
  if D.count(i) == 0:
    ans = 0
    break
  else:
    if i >= 1:
      ans *= math.pow(D.count(i-1),D.count(i))
    
print(ans % defi)

Submission status: Runtime Error

Solution code:
from math import factorial as fact
from collections import Counter

N = int( input() )
tree = list( map( int, input().split() ) )
ans = 1
if tree[0] != 0:
    print(0)
else:
    c = Counter(tree)
    if c[0] != 1:
        print(0)
    else:
        a = 1
        for i in range( 1,len(c)):
            t = c[i]
            if a == 1:
                ans *= t
            else:
                ans *= ( a ** t )
                if t != 1:
                    ans = int( ans / fact(a) )
            a = t 
        print(ans)

Submission status: Runtime Error

Solution code:
def ContinueCnt(A):
    datas, counts = [_ for _ in range(max(A) + 1)], [0] * (len(A) + 1)
    for i in range(max(A) + 1):  # i = 0 ~ max(A)
        if i not in A:
            print(0)
            exit()
        else:
            counts[i] = A.count(i)
    return datas, counts


N = int(input())
D = map(int, input().split())
D_datas, D_counts = ContinueCnt(D)
# print(D_datas, D_counts)  # for debug
INF = 998244353

# first check zero
if D_counts[0] != 1:
    print(0)
    exit()

ans = 1
# check 1 ~ max(D)
for i in range(1, max(D_datas) + 1):
    ans *= D_counts[i - 1] ** D_counts[i]
    ans %= INF
    # print("now = ", ans)  # for debug

print(ans % INF)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
my_max=-1
cnt_max=1
cnt=[0 for i in range(n)]
if a[0]!=0 or n==1:
    print(0)
    exit()
for i in range(1,n):
    if my_max<a[i]:
        my_max=a[i]
    cnt[a[i]]+=1
    if cnt[cnt_max]<cnt[a[i]]:
        cnt_max=a[i]
b=[[2 for i in range(cnt_max+1)]for j in range(my_max)]
for i in range(my_max):
    if i==0:
        for j in range(cnt_max+1):
            b[i][j]=j+2
    else:
        for j in range(cnt_max+1):
            if j==0:
                pass
            else:
                b[i][j]=b[i-1][j]+b[i][j-1]
res=cnt[1]
for i in range(2,my_max+1):
    if cnt[i]!=1:
        res*=b[cnt[i-1]-2][cnt[i]-1]
    elif i!=2 and a[i]==1:
        res*=cnt[i-1]
print(res)

Submission status: Runtime Error

Solution code:
import collections
import numpy as np
import math

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

ans = 0
flag = 0

dx = collections.Counter(d)

if(dx[0] != 1):
    flag = 1

ds = list(set(d))
if(len(ds) != ds[-1]+1):
    flag = 1
        
if(flag == 0):
    ans += 1
    for i in ds:
        ans *= math.pow(dx[i],dx[i+1])

print(round(ans)%998244353)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from collections import Counter
import math
M = 998244353
N = int(input())
# D = list(map(int, input().split()))
counter = Counter(map(int, input().split()))
ans = counter[1]
pre_c = 1
for i, (n, c) in enumerate(sorted(counter.items())):
    if i != n:
        ans = 0
        break
    elif n <= 1 or c == 1:
        pre_c = c
        continue
    elif pre_c == 1:
        ans *= c
        pre_c = c
        continue
    ans *= (math.factorial(c) ** 2 * (c+1)) // (math.factorial(c+2 - pre_c) * math.factorial(pre_c-1)) // pre_c
    pre_c = c
print(ans % M)

Submission status: Runtime Error

Solution code:
from math import factorial as fact
from collections import Counter

N = int( input() )
tree = list( map( int, input().split() ) )
ans = 1
if tree[0] != 0:
    print(0)
else:
    c = Counter(tree)
    if c[0] != 1:
        print(0)
    else:
        print(c)
        a = 1
        for i in range( 1,len(c)):
            t = c[i]
            if a == 1:
                print(ans)
                ans *= t
            else:
                print(ans)
                ans *= ( a ** t )
                if t != 1:
                    ans = int( ans / fact(a) )
            a = t 
        print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
L = [int(i) for i in input().split()]
LL = [0 for i in range(pow(10,5)+1)]
LLL = np.ones(pow(10,5)+1, dtype = "uint64")
 
for i in range(n):
  LL[L[i]] += 1
  
out = 1
for i in range(len(LL)-1):
  LLL[i+1] = pow(i+1, LL[i+1])

out = int(np.prod(LLL))
if L[0] != 0 or LL[0] != 1:
  out = 0
print(out % 998244353)

Submission status: Runtime Error

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

waru = 998244353

dic = {}
dic2 = {}

for i in range(1, N+1):
    dic[i] = D[i-1]

for number, distance in dic.items():
    if distance in dic2:
        dic2[distance] = dic2[distance] + 1
    else:
        dic2[distance] = 1

if 0 in dic2:
    if dic2[0] >= 2:
        print(0)
        exit(0)
else:
    print(0)
    exit(0)

l = []

for i in range(1, len(dic2.items()) -1):
    l.append(dic2[i] ** dic2[i+1])

r = 1
for x in l:
    r *= x
d, m = divmod(r, waru)
print(m)

Submission status: Runtime Error

Solution code:
from math import factorial as fact
from collections import Counter

N = int( input() )
tree = list( map( int, input().split() ) )
ans = 1
if tree[0] != 0:
    print(0)
else:
    c = Counter(tree)
    if c[0] != 1:
        print(0)
    else:
        print(c)
        a = 1
        for i in range( 1,len(c)):
            t = c[i]
            if a == 1:
                print(ans)
                ans *= t
            else:
                print(ans)
                ans *= ( a ** t )
                if t != 1
                    ans = int( ans / fact(a) )
            a = t 
        print(ans)