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:
import operator as op
from functools import reduce

n = int(input())

ds = map(int, input().split(' '))
ds = list(ds)
modulus = 998244353


def prod(nums, start_value=1):
    result = start_value
    for num in nums:
        result *= num

    return result


def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom


def f(q, p):
    if q == 1:
        return 1
    result = 0
    for k in range(0, p+1):
        result += ncr(p, p-k) * f(q-1, p-k)
    return result


q = ds.count(0)
if(q != 1):
    print(0)
    exit()

result = 1
for i in range(0, max(ds) + 1):
    if not i in ds:
        print(0)
        exit()

    p = ds.count(i+1)
    result *= f(q, p) % modulus
    q = p


print(int(result))

Submission status: Runtime Error

Solution code:
n=int(input())
d=[int(i) for i in input().split()]
if 0 not in d:
    print(0)
else:
    ans=1
    m=max(d)
    cnt=[0]*(10**5+1)
    for i in d:
        cnt[i]+=1

    kaijo=[1]*(10**5+1)
    for i in range(1,m+1):
        kaijo[i]=(kaijo[i-1]%998244353)*(i%998244353)%998244353

    for i in range(1,m+1):
        ans*=(cnt[i]%998244353)*(kaijo[cnt[i]-1]%998244353)
        if cnt[i]>1 and cnt[i]%cnt[i-1]!=0:
            ans*=2
        ans%=998244353
    print(ans)

Submission status: Runtime Error

Solution code:
import math

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

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


dict = {}

for i in range(len(D)):
    if D[i] not in dict:
        dict[D[i]] = 1
    else:
        dict[D[i]] += 1

for key in dict:
    if key == 0:
        break
    if dict[key] == 0:
        print(0)
        exit()
sum = 1
n = 0

#dict = list(sorted(dict.items(), key=lambda x:x[0]))
dict_dm = sorted(dict)
#print(dict)
#print(dict_dm)

for i in range(len(dict_dm) + 1):
    if i == 1:
        #print("check2")
        n = dict[i]
    elif i >=1:
        #print("check1")
        sum *= n**dict[i]
        n = dict[i]

print(sum%998244353)

Submission status: Runtime Error

Solution code:
n = int(input())
vertex_list = list(map(int, input().split()))
if vertex_list[0] != 0:
    print(0)
else:
    maxx = max(vertex_list)
    mark = 0
    for i in range(1,maxx+1):
        if i not in vertex_list:
            mark = -1
            break
    if mark == -1:
        print(0)
    else:
        ans = 1
        for vertex in vertex_list:
            if vertex is not 0:
                ans *= vertex
print(ans)

Submission status: Runtime Error

Solution code:
import operator as op
from functools import reduce

n = int(input())

ds = map(int, input().split(' '))
ds = list(ds)
modulus = 998244353


def prod(nums, start_value=1):
    result = start_value
    for num in nums:
        result *= num

    return result


def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom


def f(q, p):
    if q == 1:
        return 1
    result = 0
    for k in range(0, p+1):
        result += ncr(p, p-k) * f(q-1, p-k)
    return result


q = ds.count(0)
if(q != 1):
    print(0)
    exit()

result = 1
for i in range(0, max(ds) + 1):
    if not i in ds:
        print(0)
        exit()

    p = ds.count(i+1)
    result *= f(q, p)
    q = p


print(int(result) % modulus)

Submission status: Runtime Error

Solution code:
import math

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

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


dict = {}

for i in range(len(D)):
    if D[i] not in dict:
        dict[D[i]] = 1
    else:
        dict[D[i]] += 1

for key in dict:
    if key == 0:
        break
    if dict[key] == 0:
        print(0)
        exit()
sum = 1
n = 0

#dict = list(sorted(dict.items(), key=lambda x:x[0]))
dict_dm = sorted(dict)
#print(dict)
#print(dict_dm)

for i in range(len(dict_dm)):
    if i == 1:
        #print("check2")
        n = dict[i]
    elif i >=1:
        #print("check1")
        sum *= n**dict[i]
        n = dict[i]

print(sum%998244353)

Submission status: Runtime Error

Solution code:
N = int(input())
D = list(map(int, input().split()))
if not D[0]==0:
  ans=0
else:
  D.remove(0)
  ans = reduce(lambda x,y:x*y,D)
print(ans%998244353)

Submission status: Runtime Error

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

if D[0] != 0:
    print(0)
else:
    D.sort()

    if D[1] == 0:
        print(0)
        break

    mod = 998244353
    ans = 1
    distance = 0
    prev = 1
    now = 0
    
    for i in range(N):
        if D[i] == distance:
            now += 1
        elif D[i] == distance + 1:
            ans = (ans % mod) * (prev ** now % mod) % mod
            prev = now+0
            now = 1
            distance += 1
        else:
            ans = 0
            break
    else:
        ans = (ans % mod) * (prev ** now % mod) % mod
    
    print(ans)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N = int(input())
D = list(map(int, input().split()))
if not D[0]==0:
  ans=0
else:
	D.remove(0)
    ans = reduce(lambda x,y:x*y,D)
print(ans%998244353)

Submission status: Runtime Error

Solution code:
# メモ
# 連結無効グラフ　＝　全部が繋がっている

# N = int(input())
# D = list(map(int, input()))

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

JOSU = 998244353

anser = 0

deep = [-1] * (N + 1)
deep[0] = 0
memo = [0] * N

# 各深さの数を求める
for i in range(N):
    if deep[D[i]] == -1:
        deep[D[i]] = 0
    deep[D[i]] += 1

def dfs(n, end, sum):
    if n == end:
        return sum
    elif deep[n] == -1 or deep[n + 1] == -1:
        return dfs(n + 1, end, sum)
    else:
        return dfs(n + 1, end, sum * deep[n] ** deep[n + 1])

print(dfs(0, N, 1) % JOSU)

Submission status: Runtime Error

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

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

#output
from collections import Counter
l = Counter(D).most_common()
l = sorted(l, key = lambda x:x[0])
M = len(l)

if M > 1:
    for i in range(l[1][1]):
        answer *= i
    else:
        answer = 1
if l[0] != (0, 1):
    answer = 0
else:
    for i in range(1, M-1):
        answer *= pow(l[i][1],l[i+1][1])
print(answer % 998244353)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import operator as op
from functools import reduce

n = int(input())

ds = map(int, input().split(' '))
ds = list(ds)
modulus = 998244353


def prod(nums, start_value=1):
    result = start_value
    for num in nums:
        result *= num

    return result


def ncr(n, r):
    r = min(r, n-r)
    numer = reduce(op.mul, range(n, n-r, -1), 1)
    denom = reduce(op.mul, range(1, r+1), 1)
    return numer / denom


def f(q, p):
    if q == 1:
        return 1

    return sum([ncr(p, p-k) * f(q-1, p-k) for k in range(0, p+1)])


q = ds.count(0)
if(q != 1):
    print(0)
    exit()

result = 1
for i in range(0, max(ds) + 1):
    if not i in ds:
        print(0)
        exit()

    p = ds.count(i+1)
    result *= f(q, p)
    q = p


print(int(result) % modulus)

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) != 0 else n
    if len(idx) == n - minidx and (idx == np.arange(minidx, n)).all():
        inner = [counts[i] ** counts[i + 1] for i in range(minidx)]
        return int(np.prod(inner)) % MAXINT
    return 0


if __name__ == '__main__':
    # A 問題
    # n = int(input())
    # print(q1(n))

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

Submission status: Runtime Error

Solution code:
n = int(input())
vertex_list = list(map(int, input().split()))
if vertex_list[0] != 0:
    print(0)
else:
    maxx = max(vertex_list)
    list = []
    mark = 0
    ans = 1
    for i in range(1,maxx+1):
        count = vertex_list.count(i)
        if count == 0:
            marc == 1
        ans *= (i) ** count
    if mark == -1:
        print(0)
    else:
        print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from itertools import groupby
import numpy as np
def print_ans(N, input_line):
    """Test Case
    >>> print_ans(4, "0 1 1 2")
    2
    >>> print_ans(4, "1 1 1 1")
    0
    >>> print_ans(7, "0 3 2 1 2 2 1")
    24
    """
    ar = list(map(int,input_line.split()))
    ngflg = False
    if ar[0] != 0:
        ngflg = True
    ar = sorted(ar[1:])
    check_ar = list(set(ar))
    if check_ar[0] == 0:
        ngflg = True
    # for i in range(len(check_ar) - 1):
    #     if check_ar[i] + 1 != check_ar[i + 1]:
    #         ngflg = True
    ar = np.array(ar)
    # tmp = np.split(ar, np.cumsum(np.unique(ar, return_counts=True)[1])[:-1])

    if ngflg:
        print("0")
    else:
         ans_ar= np.array([int(k) ** len(list(g)) for k, g in groupby(ar)])
        print(np.prod(ans_ar) % 998244353)

if __name__ == '__main__':
    N = int(input().rstrip())
    ar = input().rstrip()
    print_ans(N, ar)

Submission status: Runtime Error

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

dic = {}
for i in d:
    try:
        a = dic[i]
        dic[i] = a + 1
    except:
        dic[i] = 1
ans = 0
if max(d) > 1:
    ans = 1
    for j in range(max(d)+1)[2:]:
        ans *= dic[j-1]**dic[j]

print(ans%div)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

MOD = 998244353  # type: int

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

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

    if D[0]!=0:
        print(0)
    else:
        answer=1
        bak=cn[0]
        kyo=1
        hen=1
        while True:
            cnt=cn[kyo]
            hen+=cnt
            if cnt==0:
                break
            answer=answer*baai(bak, cnt)%MOD
            bak=cnt
            kyo+=1
        if hen==N:
            print(answer%MOD)
        else:
            print(0)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, D)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math

s = raw_input().split()
[N] = s
N = int(N)

s = raw_input().split()
D = [0]*N

for i in range(N):
    D[i] = int(s[i])


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

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

D.sort()
#print D
m = D[N-1]
#print m
count = [0]*(m+1)

for i in range(N):
    count[D[i]]+=1
    #print "add",D[i]

#print count
for i in range(m+1):
    if (count[i] == 0):
        print 0
        exit(0)


res = 1
for i in range(m):
    res *= math.pow(count[i],count[i+1])

print int(res)

Submission status: Runtime Error

Solution code:
n=int(input())
d=[int(i) for i in input().split()]
if 0 not in d:
    print(0)
else:
    ans=1
    m=max(d)
    cnt=[0]*(10**5+1)
    for i in d:
        cnt[i]+=1

    kaijo=[1]*(10**5+1)
    for i in range(1,m+1):
        kaijo[i]=(kaijo[i-1]%998244353)*(i%998244353)%998244353

    for i in range(1,m+1):
        ans*=(cnt[i]%998244353)*(kaijo[i-1]%998244353)
        if i-1>0 and cnt[i]%cnt[i-1]!=0 and cnt[i]!=1:
            ans*=2
        ans%=998244353
    print(ans)

Submission status: Runtime Error

Solution code:
# 指定された距離で何個の木を作成することができるかを計算する
import bisect
import itertools
N = int(input())
D = list(map(int, input().split()))
#
D=list(map(int,inp.split()))
print(D)

node_dis = [1] # 距離iのノードの個数をi番目の要素として格納したリスト
flag_none = False

# 頂点１から深さは決まっている.深さni毎に前のni-1のノードから重複を許してc_ni個選んでノードに繋げばよい
if not D[0] == 0:
    print(0)
    flag_none = True

else:
    D.sort()  # 昇順でソート
    for i in range(1,N):
        if i == 1 and bisect.bisect_left(D,1) >= 2:  # 0が２つ以上ある場合
            print(0)
            flag_none = True
            break
        else:
            index = bisect.bisect_left(D,i+1)
            new_num = index - sum(node_dis)
            node_dis.append(new_num)
            if new_num == 0:
                if not index == N:
                    print(0)
                    flag_none = True
                break

if flag_none == False:
    sum = 1
    for (ind, di) in enumerate(node_dis): 
        if ind > 0:
            sum *= node_dis[ind-1] ** di
    print(sum)

Submission status: Runtime Error

Solution code:
# 入力
N = int(input())
D = [int(i) for i in list(input().split())]

result = 1
temp1 = 1
for i in range(max(D)+1):
    if D.count(i) == 0:
        print(0)
        exit()
    elif i == 1:
        result *= D.count(1)
        temp1 = D.count(1)
    else:
        temp2 = D.count(i)
        result *= temp1**temp2
        temp1 = temp2
print(int(result/2%998244353))

Submission status: Runtime Error

Solution code:
import math

s = raw_input().split()
[N] = s
N = int(N)

s = raw_input().split()
D = [0]*N

for i in range(N):
    D[i] = int(s[i])


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

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

D.sort()
#print D
m = D[N-1]
#print m
count = [0]*(m+1)

for i in range(N):
    count[D[i]]+=1
    #print "add",D[i]

#print count
res = 1
for i in range(m):
    res *= math.pow(count[i],count[i+1])

print int(res)

Submission status: Runtime Error

Solution code:
from fractions import gcd
import os
import math
import sys
from math import factorial
input = sys.stdin.readline


if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 998244353

g1 = [1, 1]  # 元テーブル
g2 = [1, 1]  # 逆元テーブル
inverse = [0, 1]  # 逆元テーブル計算用テーブル


def cmb(n, r, mod):
    return factorial(n) / factorial(r) / factorial(n - r)
    # if (r < 0 or r > n):
    #     return 0
    # r = min(r, n-r)
    # return g1[n] * g2[r] * g2[n-r] % mod


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

    for (k, v) in enumerate(d):
        D.append((v, k+1))

    distance = [0] * (max(d) + 1)

    for d in D:
        distance[d[0]] += 1

    ans = 1
    for i in range(0, len(distance)):
        if i == 0:
            ans = 1
            # print(ans)
        else:
            n = distance[i-1]
            r = distance[i]
            c = cmb(n+r-1, r-1, MOD)
            # print('n', n, 'r', r, 'c', c)
            ans *= n ** r
            ans = ans % MOD
            # print(ans)

    print(ans)
    # print(*distance)
    # n = 2
    # r = 3
    # print(cmb(n+r-1, r-1, MOD))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#from math import factorial
n = int(input())

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

D.sort()
#print(D)
num = 1
m = 04
flag = 1
mb = 0
for i in D:
    if i == 0:
        mb += 1
    else:
        break
#print(mb)
S = mb

for i in D:
    if i == 0:
        pass
    elif i == num:
        m += 1
    elif i != num:
#        print("NUM",num,"m",m,"MB",mb)
        if flag == 0:
            pass
        a = mb ** m
        S *= a
        mb = m
        m = 1
        num += 1
        while num not in D:
            num += 1
            if max(D) < num:
                break
#            print(num)
a = mb ** m
S *= a
print(S % 998244353)
        
#998244353

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
d = list(map(int, input().split()))
m = {}
ans = 1

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

for i in range(n):
    num = d[i]
    if num in m:
        m[num] += 1
    else:
        m[num] = 1

#print(m)
#print(max(m))

#for i in range(1, max(m)+1):
for i in range(0, max(m)):
    if i not in m:
        print(0)
        sys.exit()
    prevCnt = m[i]
    curCnt = m[i+1]
    # prevCnt = m[i] % 998244353
    #curCnt = m[i+1] % 998244353
    ans *= prevCnt ** curCnt
    #ans *= (prevCnt ** curCnt) % 998244353

print(ans % 998244353)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
d = list(map(int, input().split()))
m = {}
ans = 1

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

for i in range(n):
    num = d[i]
    if num in m:
        m[num] += 1
    else:
        m[num] = 1

#print(m)
#print(max(m))

#for i in range(1, max(m)+1):
for i in range(0, max(m)):
    if i not in m:
        print(0)
        sys.exit()
    prevCnt = m[i]
    curCnt = m[i+1]
    # prevCnt = m[i] % 998244353
    #curCnt = m[i+1] % 998244353
    # ans *= prevCnt ** curCnt
    ans *= (prevCnt ** curCnt) % 998244353

print(ans % 998244353)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
d = list(map(int, input().split()))
m = {}
ans = 1

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

for i in range(n):
    num = d[i]
    if num in m:
        m[num] += 1
    else:
        m[num] = 1

#print(m)
#print(max(m))

#for i in range(1, max(m)+1):
for i in range(0, max(m)):
    if i not in m:
        print(0)
        sys.exit()
    # prevCnt = m[i]
    # curCnt = m[i+1]
    prevCnt = m[i] % 998244353
    curCnt = m[i+1] % 998244353
    # ans *= prevCnt ** curCnt
    ans *= (prevCnt ** curCnt) % 998244353

print(ans % 998244353)

Submission status: Runtime Error

Solution code:
n = int(input())
d = list(map(int,input().split()))
if d[0] > 0:
  print("0")
d.remove(0)
ans = 1
for i in range(len(d)):
  ans *= i
print(i)

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():
        inner = [counts[i] ** counts[i + 1] for i in range(minidx)]
        return int(np.prod(inner)) % MAXINT
    return 0


if __name__ == '__main__':
    # A 問題
    # n = int(input())
    # print(q1(n))

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

Submission status: Runtime Error

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

if l[0] != 0:
  ans = 0
del l[0]
if l.count(0) != 0:
  ans = 0
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)*l.count(i)
  ans = a % 998244353
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 1
if n == 1:
  if l[0] == 0:
    print(1)
  else:
    print(0)

elif n == 2:
  if l[0] ==0 and l[1] == 1:
    print(1)
  else:
    print(0)

else:
  if l[0] != 0:
    print(0)
  else:
    a = [0]*n
    for i in range(n):
      a[l[i]] += 1

    if a[0] != 1:
      print(0)
    else:
      for i in range(n-1):
        if a[i]!=0 and a[i+1]!=0:
          ans = ans * (a[i]**a[i+1])
        else:
          break

      print(ans%n = int(input())
l = list(map(int,input().split()))
ans = 1
if n == 1:
  if l[0] == 0:
    print(1)
  else:
    print(0)

elif n == 2:
  if l[0] ==0 and l[1] == 1:
    print(1)
  else:
    print(0)

else:
  if l[0] != 0:
    print(0)
  else:
    a = [0]*n
    for i in range(n):
      a[l[i]] += 1

    if a[0] != 1:
      print(0)
    else:
      for i in range(n-1):
        if a[i]!=0 and a[i+1]!=0:
          ans = ans * (a[i]**a[i+1])
        else:
          break

      print(ans%998244353)

Submission status: Runtime Error

Solution code:
import functools
import operator 
N = int(input())
D = list(map(int, input().split()))
ans=1
if not D[0]==0:
  ans=0
else:
	D.remove(0)
    ans = functools.reduce(operator.mul, D)
    print(ans%998244353)

Submission status: Runtime Error

Solution code:
def main():
  n = int(raw_input())
  items = map(int, raw_input().split())

  if items[0] > 0:
      print(0)
      return

  elems = []
  max_h = 0
  for i, e in enumerate(items):
    elems.append((i, e))
    if e > max_h:
      max_h = e
  
  graph = {}
  for i, e in sorted(elems, key=lambda x: x[1]):
    if e not in graph:
      graph[e] = []
    graph[e].append(i)
  
  dp = [0, ] * (max_h + 1)
  dp[0] = 1
  dp[1] = 1

  for i in xrange(2, max_h+1):
      total = len(graph[i])
      prev = len(graph[i-1])
      dp[i] = dp[i-1] * prev**total

  print(dp[max_h])

main()

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
d = list(map(int, input().split()))
m = {}
ans = 1

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

for i in range(n):
    num = d[i]
    if num in m:
        m[num] += 1
    else:
        m[num] = 1

#print(m)
#print(max(m))

#for i in range(1, max(m)+1):
for i in range(0, max(m)):
    if i not in m:
        print(0)
        sys.exit()
    prevCnt = m[i]
    curCnt = m[i+1]
    ans *= prevCnt ** curCnt

print(ans % 998244353)

Submission status: Runtime Error

Solution code:
N = int(input())

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

mydic = {}
myans = 0

for i in D:
    if i in mydic:
        mydic[i] += 1
    else:
        mydic[i] = 1

#dicsorted = sorted(mydic.items())

myans = 1

if 0 not in mydic:
    print(0)
else:
    for i in range(len(mydic)):
        if i ==0:
            i = 1
        myans = myans * (i**mydic[i])
    print(myans%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:
        if b==1:
            return a
        else:
            ans=pow(a,b-1)*a
            ans=ans%998244353
            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:
import math
n = int(input())
d = list(map(int, input().split()))
b = 1
ans = 1
maxd = max(d)
if d[0] == 0 and d.count(0) == 1:
    for i in range(1, maxd+1):
        dc = d.count(i)
        if dc == 0:
            print(0)
            exit()
        ans *= math.pow(b, dc)
        b = dc
    print(ans % 998244353)
else:
    print(0)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
d = list(map(int, input().split()))
m = {}
ans = 1

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

for i in range(n):
    num = d[i]
    if num in m:
        m[num] += 1
    else:
        m[num] = 1

#print(m)
#print(max(m))

#for i in range(1, max(m)+1):
for i in range(0, max(m)):
    if i not in m:
        print(0)
        sys.exit()
    prevCnt = m[i]
    curCnt = m[i+1]
    ans *= prevCnt ** curCnt

print(ans)

Submission status: Runtime Error

Solution code:
n,*a=map(int,open(0).read().split())
b=a.pop(0)
if b!=0:print(0);exit()
import collections
d=collections.Counter(a)
e=[[i,j] for i,j in d.items()]
e.sort()
ans=1
d1=[i[0] for i in e]
d2=[i[1] for i in e]
for i,I,j,J in zip(d1,d1[1:],d2,d2[1:]):
    if i+1!=I:
        print(0)
        RE
    ans*=j**J
print(ans)

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:
        if b==1:
            return a
        else:
            ans=pow(a,b-1)*a
            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:
import functools
import operator 
N = int(input())
D = list(map(int, input().split()))
ans=1
if not D[0]==0:
  ans=0
else:
	D.remove(0)
    ans = functools.reduce(operator.mul, D)
print(ans%998244353)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys

MOD = 998244353  # type: int

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

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

    if D[0]!=0:
        print(0)
    else:
        answer=1
        bak=cn[0]
        kyo=1
        hen=bak
        while True:
            cnt=cn[kyo]
            if cnt==0:
                break
            answer*=baai(bak, cnt)
            bak=cnt
            kyo+=1
        print(answer%MOD)
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    D = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, D)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
D_list = [int(s) for s in input().split(" ")]

res = 1
for i in range(0, max(D_list)):
    if((D_list.count(i)!=0) and (D_list.count(i+1)!=0):
        res *= D_list.count(i) ** D_list.count(i+1)
    else:
       res = 0
       break

print(res % 998244353)

Submission status: Runtime Error

Solution code:
import functools
import operator 
N = int(input())
D = list(map(int, input().split()))
ans=1
if not D[0]==0:
  ans=0
else:
	D.remove(0)
    ans = functools.reduce(operator.mul, D)
print(ans/998244353)

Submission status: Runtime Error

Solution code:
import sys
n = int(input())
d = list(map(int, input().split()))
m = {}
ans = 1

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

for i in range(n):
    num = d[i]
    if num in m:
        m[num] += 1
    else:
        m[num] = 1

#print(m)
#print(max(m))

#for i in range(1, max(m)+1):
for i in range(0, max(m)):
    prevCnt = m[i]
    curCnt = m[i+1]
    ans *= prevCnt ** curCnt

print(ans)

Submission status: Runtime Error

Solution code:
import math

N = int(input())
D = list(map(int, input().split()))
L = list()
result = 1
m = max(D)+1

for i in range(1, m):
  L.append(D.count(i))

for j in range(1,len(L)):
  result *= math.pow(L[j-1], L[j])

if D.count(0) == 1 and D[0] == 0:
  print(result % 998244353)
else:
  print(0)

Submission status: Runtime Error

Solution code:
import functools
import operator
result = functools.reduce(operator.mul, li)

N = int(input())
D = list(map(int, input().split()))
ans=1
if not D[0]==0:
  ans=0
else:
	D.remove(0)
    ans = functools.reduce(operator.mul, D)
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
elif 0 in array and array[0] != 0
    count = 0

else:
  count = 1
for i in range(N//2 + 1):
    if array[i] != 0:
        count = count * array[i]
    if array[N-i-1] !=0:
        count  = count * array[N-i-1]
if N % 2 !=0:
    if array[(N//2)] != 0:
        count = count // array[(N//2)]
            
ans = count % 998244353 
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
from itertools import groupby
import numpy as np
import pandas as pd
def print_ans(N, input_line):
    """Test Case
    >>> print_ans(4, "0 1 1 2")
    2
    >>> print_ans(4, "1 1 1 1")
    0
    >>> print_ans(7, "0 3 2 1 2 2 1")
    24
    """
    ar = list(map(int,input_line.split()))
    ngflg = False
    if ar[0] != 0:
        ngflg = True
    ar = sorted(ar[1:])
    check_ar = list(set(ar))
    if check_ar[0] == 0:
        ngflg = True
    for i in range(len(check_ar) - 1):
        if check_ar[i] + 1 != check_ar[i + 1]:
            ngflg = True
    if ngflg:
        print("0")
    else:
        ans_ar= np.array([int(k) ** len(list(g)) for k, g in groupby(ar)])
        ans = 1
        # for k, g in groupby(ar):
        #         ans *= int(k) ** len(list(g))
        print(np.prod(ans_ar) % 998244353)

if __name__ == '__main__':
    N = int(input().rstrip())
    ar = input().rstrip()
    print_ans(N, ar)

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)