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: Time Limit Exceeded

Solution code:
#                         author:  kagemeka 
#                         created: 2019-11-09 21:20:16(JST)
### modules
## from standard library
import sys
# import collections
# import math
# import string
import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
## from external libraries
# import scipy.special   
# import scipy.misc      
# import numpy as np 

mod = 998244353

def main():
    n, *d = (int(x) for x in sys.stdin.read().split())
    
    if d[0] != 0:
        print(0)
        exit()

    del d[0]

    if 0 in d:
        print(0)
        exit()

    d.sort()
    
    maximum = d[-1]
    for i in range(1, maximum + 1):
        if not i in d:
            print(0)
            exit()
            
    total = 1
    for i in range(1, maximum):
        base = bisect.bisect_right(d, i) - bisect.bisect_left(d, i)
        exp = bisect.bisect_right(d, i+1) - bisect.bisect_left(d, i+1)
        total *= (base ** exp)
    print(total % mod)
        


if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
d=list(map(int,input().split()))
c=[0]*(n+1)
ans=0
for i in range(0,n):
  c[d[i]]+=1

if d[0]!=0 or c[0]!=1:
  print(0)
  
else:
  for j in range(1,n):
    if c[j]==0 and c[j+1]==0:
      ans+=0
    else:
      if j==1:
        ans+=1
      else:
        ans*=c[j-1]**c[j]
  print(ans%998244353)

Submission status: Time Limit Exceeded

Solution code:
#                         author:  kagemeka 
#                         created: 2019-11-09 21:20:16(JST)
### modules
## from standard library
import sys
# import collections
# import math
# import string
import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator
## from external libraries
# import scipy.special   
# import scipy.misc      
# import numpy as np 

mod = 998244353

def main():
    n, *d = (int(x) for x in sys.stdin.read().split())
    
    if d[0] != 0:
        print(0)
        exit()

    del d[0]

    if 0 in d:
        print(0)
        exit()

    d.sort()
    
    maximum = d[-1]
    for i in range(1, maximum + 1):
        if not i in d:
            print(0)
            exit()
            
    total = 1
    for i in range(1, maximum):
        base = bisect.bisect_right(d, i) - bisect.bisect_left(d, i)
        exp = bisect.bisect_right(d, i+1) - bisect.bisect_left(d, i+1)
        total *= base ** exp % mod
    print(total % mod)
        


if __name__ == "__main__":
    # execute only if run as a script
    main()

Submission status: Time Limit Exceeded

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

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

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=mul*n**m
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n = int(input())
D = np.array(list(map(int, input().strip().split())))
s = list(set(D))

if len(D) == 1:
    if D[0] == 0:
        print(1)
else:
    if D[0] == 0 and (np.count_nonzero(D) == len(D)-1) and (s[-1] == len(s)-1):
        ans = 1
        for i in range(2,s[-1]+1):
            ans *= np.count_nonzero(D == i-1)**np.count_nonzero(D == i)
        print(ans % 998244353)
    else:
        print(0)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
ans = 1
D_n = [D.count(i) for i in range(max(D) + 1)]
#print(D_n)
#n1が0ではない、もしくは他に0がある
if D[0] != 0 or D.count(0) != 1:
  ans = 0
else:
  for i in range(1, max(D)):
    #print(D[i + 1])
    #print(bi)
    for j in range(D_n[i + 1]):
      ans *= D_n[i]
      if ans >= mod:
        ans %= mod
print(ans % mod)

Submission status: Time Limit Exceeded

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

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

ans=None

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

    mul=1
    n=1

    L=[]

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

    for m in L:
        mul*=n**m
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
d = list(map(int, input().split()))
b = 1
ans = 1
if d[0] == 0 and d.count(0) == 1:
    for i in range(1, max(d)+1):
        dc = d.count(i)
        if dc == 0:
            print(0)
            exit()
        if dc == 1:
            ans *= b
        else:
            if b != 1:
                ans *= pow(b, dc)
        b = dc
    print(ans % 998244353)
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x % 998244353
        x *= x % 998244353
        n //= 2

    return K * x % 998244353

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

k = max(A)

R = [1]
result = 1


for i in range(k):
    R.append(A.count(i + 1))

for j in range(k):
    result *= pow_k(R[j], R[j + 1])

    # if R[j + 1] % 2 == 0:
    #     result *= ((R[j] ** 2 % 998244353) ** (R[j + 1] // 2)) % 998244353
    # else:
    #     result *= R[j] * ((R[j] ** 2 % 998244353) ** (R[j + 1] // 2)) % 998244353

    result %= 998244353

    if result == 0:
        print(0)
        exit()

print(result % 998244353)

Submission status: Time Limit Exceeded

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

if d[0] != 0:
  print(0)
else:
  M = max(d) + 1
  ans = 1
  for i in range(M-2):
    ans = ans * (d.count(i+1) ** d.count(i+2)) % 998244353
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

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

k = max(A)

R = [1]
result = 1


for i in range(k):
    R.append(A.count(i + 1))

for j in range(k):
    result *= pow_k(R[j], R[j + 1])

    # if R[j + 1] % 2 == 0:
    #     result *= ((R[j] ** 2 % 998244353) ** (R[j + 1] // 2)) % 998244353
    # else:
    #     result *= R[j] * ((R[j] ** 2 % 998244353) ** (R[j + 1] // 2)) % 998244353

    result %= 998244353

    if result == 0:
        print(0)
        exit()

print(result % 998244353)

Submission status: Time Limit Exceeded

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


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

m=[1]
p=1


for i in range(1,max(d)+1):
    if d.count(i)==0:
        print(0)
        sys.exit(0)
    elif d[i]==0:
        print(0)
        sys.exit(0)
    else:
        tmp = 1
        for j in range(d.count(i)):
            tmp = (tmp * d.count(i-1)%998244353)%998244353
            
        p = (p * tmp)%998244353
    

print(p)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
if a[0] != 0 or a.count(0) != 1:
    print(0)
else:
    s = 1
    for i in range(n-1):
        s *= a.count(i) ** a.count(i+1)
    print(s % 998244353)

Submission status: Time Limit Exceeded

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

mem = [-1 for i in range(n+1)]
def countd(y):
  if mem[y] == -1:
    mem[y] = d.count(y)
  return mem[y]

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

count = 1
for i in range(n-1):
  x = d[i+1]
  if x == 0:
    print(0)
    exit()
  if x == 1:
    continue
  count = count * countd(x-1)
  count = count % 998244353

print(count)

Submission status: Time Limit Exceeded

Solution code:
def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

N = int(input())
D = list(map(int,input().split()))
ans = 1
a = 0
if D[0] != 0 or D.count(0) > 1:
  print(0)
  exit()

prev = 1
for i in range (max(D)):
  if D.count(i+1) == 0:
    if a < N-1:
      print(0)
      exit()
    else:
      print(ans)
      exit()
  else:
    count = D.count(i+1)
    a += count
    ans = ( ans * pow_k(prev,count) ) %998244353 
    prev = count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import Counter, deque, defaultdict
from math import factorial
from fractions import Fraction
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10**15 +5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))


MOD = 998244353
n = INT()
d = LIST()
if d[0] != 0:
    print(0)
    sys.exit()
d = d[1:]
ma = max(d)
mi = min(d)
res = 1
for i in range(1, ma+1):
    if d.count(i) == 0:
        print(0)
        sys.exit()
    if i == 1:
        temp = d.count(1)
    else:
        temp2 = d.count(i)
        res *= temp**temp2
        temp = temp2

print(res%MOD)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin
n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]
if a[0] != 0 or a.count(0) != 1:
    print(0)
else:
    list = []
    for i in range(n):
        list.append(a.count(i))
    s = 1
    for i in range(n-1):
        s *= list[i]** list[i+1]
    print(s % 998244353)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = [int(x) for x in input().split()]
D = sorted(D)
if D[0] > 0:
    print(0)
else:
    new_l = list()
    for n in range(1,N+1):
        count = 0
        for d in D:
            if d == n:
                count += 1
        new_l.append(count)
    res = 1
    for n in range(len(new_l)-1):
        res *= new_l[n]**new_l[n+1]
        res = res%998244353
    print(res)

Submission status: Time Limit Exceeded

Solution code:
def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

N = int(input())
D = list(map(int,input().split()))
ans = 1
a = 0
if D[0] != 0 or D.count(0) > 1:
  print(0)
  exit()

prev = 1
for i in range (N-2):
  if D.count(i+1) == 0:
    if a < N-1:
      print(0)
      exit()
    else:
      print(ans)
      exit()
  else:
    count = D.count(i+1)
    a += count
    ans = ( ans * pow_k(prev,count) ) %998244353 
    prev = count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

k = max(A)

R = [1]
result = 1


for i in range(k):
    R.append(A.count(i + 1))

for j in range(k):
    if R[j + 1] % 2 == 0:
        result *= ((R[j] ** 2 % 998244353) ** (R[j + 1] // 2)) % 998244353
    else:
        result *= R[j] * ((R[j] ** 2 % 998244353) ** (R[j + 1] // 2)) % 998244353

    result %= 998244353

    if result == 0:
        print(0)
        exit()

print(result % 998244353)

Submission status: Time Limit Exceeded

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


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

m=[1]
p=1


for i in range(1,max(d)+1):
    if collections.Counter(d)[i]==0:
        print(0)
        sys.exit(0)
    elif d[i]==0:
        print(0)
        sys.exit(0)
    else:
        tmp = 1
        for j in range(collections.Counter(d)[i]):
            tmp = (tmp * collections.Counter(d)[i-1]%998244353)%998244353
            
        p = (p * tmp)%998244353
    

print(p)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

k = max(A)

R = [1]
result = 1


for i in range(k):
    R.append(A.count(i + 1))

for j in range(k):
    if R[j + 1] % 2 == 0:
        result *= ((R[j] ** 2) ** (R[j + 1] // 2))
    else:
        result *= R[j] * ((R[j] ** 2) ** (R[j + 1] // 2))
        
    result %= 998244353

    if result == 0:
        print(0)
        exit()

print(result % 998244353)

Submission status: Time Limit Exceeded

Solution code:
mod = 998244353
N = int(input())
D = list(map(int, input().split()))
cnt_list = []
if D[0] != 0:
    print(0)
    exit()
for i in range(0, N):
    tmp = D.count(i)
    if tmp == 0:
        break
    cnt_list.append(tmp)
##print(cnt_list)
if len(cnt_list) != len(set(D)):
    print(0)
    exit()
if cnt_list[0] != 1:
    print(0)
    exit()
##print(cnt_list)
ans = 1
for i in range(1, len(cnt_list)):
    ans *= cnt_list[i-1] ** cnt_list[i]
    ans = ans % mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = [int(i) for i in input().split()]
if D[0]!=0:
    print(0)
else:
    list_num = [0]*(N)
    for j in range(1,N):
        for k in range(0,N):
            if D[j]==k:
                list_num[k]+=1
    if list_num[0]!=0:
        print(0)
    else:
        l = 1
        while(True):
            if list_num[N-l]==0:
                list_num.pop()
                l+=1
            else:
                break
        len_list = len(list_num)-1
        if len_list==0:
            print(1)
        elif len_list==1:
            print(list_num[1])
        else:
            S=1
            for m in range(1,len_list):
                S = S*(list_num[m]**list_num[m+1])
            print(S%998244353)

Submission status: Time Limit Exceeded

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

INF = 998244353

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

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

C = [1]
res = 1
pre = 1

Dmax = max(D)

for i in range(1,Dmax+1):
    now = D.count(i)
    res = (res * pre ** now) % INF
    pre = now

# for i in range(1,Csize):
#     res = (res * C[i-1] ** C[i]) % INF

print(res)

Submission status: Time Limit Exceeded

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)
            output %= 998244353
    print(output)

Submission status: Time Limit Exceeded

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

import pdb
import sys
F = sys.stdin
N = int(F.readline())
d = list(map(int,F.readline().rstrip().split()))
ans_no = 0
ans = 1
max = max(d)
e = 998244353
def power(a,b): #aのb乗をeで割った余りを返す
    if b == 0:
        return 1
    elif b == 1:
        return a % e
    elif b%2 == 0:
        return power(a,b//2)**2 % e
    else:
        return power(a,b//2)**2*a % e

def ans_no():
    print(0)
    sys.exit()

if d[0] != 0:
    ans_no()

for i in range(max+1):
    count = d.count(i)
    if i == 0 and count > 1:
        ans_no()
    if count == 0:
        ans_no()

    if i == 0 or i == 1:
        continue
    count_before = d.count(i-1)
    ans = (ans * power(count_before, count)) % e

print(ans % e)

Submission status: Time Limit Exceeded

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

INF = 998244353

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

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

C = [1]
res = 1

Dmax = max(D)

for i in range(1,Dmax+1):
    temp = D.count(i)
    C.append(temp)
    res = (res * C[i-1] ** C[i]) % INF

# for i in range(1,Csize):
#     res = (res * C[i-1] ** C[i]) % INF

print(res)

Submission status: Time Limit Exceeded

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

def pow_r(x, n):
  if n == 0:
    return 1
  if n % 2 == 0:
    return pow_r(x ** 2, n // 2)
  else:
    return x * pow_r(x ** 2, (n-1) // 2)

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

for j in range(1,len(L)):
  #result *= L[j-1] ** L[j]
  result *= pow_r(L[j-1], L[j])

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

Submission status: Time Limit Exceeded

Solution code:
import sys
import collections
def solve():
    sys.setrecursionlimit(100000)
    mod = 998244353

    N = int(input())
    D = list(map(int, input().split()))
    cnter = collections.Counter(D)
    cnter = sorted(cnter.items())

    ans = 1
    if cnter[0][0] != 0 or cnter[0][1] != 1 : 
        ans = 0
    elif cnter[1][0] != 1:
        ans = 0
    else:
        for i in range(2, len(cnter)):
            if cnter[i][0] != i:
                ans = 0
                break

            else:
                ans = ans * modpow(cnter[i-1][1], cnter[i][1], mod) % mod
    
    print(ans)


def modpow(a, b, mod):
    res = 1
    while b > 0:
        if b & 1:
            res = res * a % mod
            a = a * a % mod
            b = b >> 1
    return res

if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

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

import pdb
import sys
F = sys.stdin
N = int(F.readline())
d = list(map(int,F.readline().rstrip().split()))
ans_no = 0
ans = 1
max = max(d)
e = 998244353
def power(a,b): #aのb乗をeで割った余りを返す
    if b == 0:
        return 1
    elif b == 1:
        return a % e
    elif b%2 == 0:
        return power(a,b//2)**2 % e
    else:
        return power(a,b//2)**2*a % e

def ans_no():
    print(0)
    sys.exit()

if d[0] != 0:
    ans_no()

for i in range(max+1):
    count = d.count(i)
    if i == 0 and count > 1:
        ans_no()
    if count == 0:
        ans_no()

    if i == 0 or i == 1:
        continue
    count_before = d.count(i-1)
    ans = ans * power(count_before, count)

print(ans % e)

Submission status: Time Limit Exceeded

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

m=[1]
p=1


for i in range(1,max(d)+1):
    if collections.Counter(d)[i]==0:
        print(0)
        sys.exit(0)
    elif d[i]==0:
        print(0)
        sys.exit(0)
    else:
        m.append(collections.Counter(d)[i]) 
    
for i in range(1, len(collections.Counter(d))):
    p = ((p%998244353) * ((m[i-1]**m[i])%998244353))%998244353
    
    

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

Submission status: Time Limit Exceeded

Solution code:
def pow_k(x, n):
    """
    O(log n)
    """
    if n == 0:
        return 1

    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2

    return K * x

N = int(input())
D = list(map(int,input().split()))
ans = 1
if D[0] != 0 or D.count(0) > 1:
  print(0)
  exit()

prev = 1
for i in range (N-2):
  count = D.count(i+1)
  ans = ( ans * pow_k(prev,count) ) %998244353 
  prev = count
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

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

k = max(A)

c = 1
R = [1]
result = 1


for i in range(k):
    R.append(A.count(i + 1))
    c += 1

for j in range(k):
    result *= R[j] ** R[j + 1]
    result %= 998244353

    if result == 0:
        print(0)
        exit()

print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = list(map(int, input().split()))
E = [D.count(1)]
answer = 1

if D[0] != 0:
  print('0')
else:
  for i in range(1, max(D)):
    E.append(D.count(i + 1))
    answer *= E[i - 1] ** E[i]
  print(answer % 998244353)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = list(map(int,input().split()))
ans = 1
if D[0] != 0 or D.count(0) > 1:
  print(0)
  exit()

prev = 1
for i in range (N-2):
  count = D.count(i+1)
  ans = ( ans * (prev**count)  ) %998244353 
  prev = count
print(ans)

Submission status: Time Limit Exceeded

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

if d[0] != 0:
  print(0)
else:
  M = max(d) + 1
  ans = 1
  for i in range(M-2):
    ans = ans * (d.count(i+1) ** d.count(i+2)) % 998244353
  print(ans)

Submission status: Time Limit Exceeded

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

INF = 998244353

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

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

C = []

Dmax = max(D)

for i in range(Dmax+1):
    temp = D.count(i)
    C.append(temp)

Csize = len(C)

res = 1

for i in range(1,Csize):
    res = (res * C[i-1] ** C[i]) % INF

print(res)

Submission status: Time Limit Exceeded

Solution code:
mod = 998244353
N = int(input())
D = list(map(int, input().split()))
if D[0] != 0 or D.count(0) > 1:
    print(0)
    exit()
cnt_list = [0] * (N-1)
for i in range(0, N-1):
    cnt_list[i] = D.count(i+1)
##print(cnt_list)
ans = 1
reach = False
for l in cnt_list:
    if l == 0:
        reach = True
    if l != 0 and reach == True:
        print(0)
        exit()
for i in range(1, len(cnt_list)):
    if cnt_list[i] == 0:
        break
    ans *= cnt_list[i-1] ** cnt_list[i]
    ans = ans % mod
print(ans)

Submission status: Time Limit Exceeded

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

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

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)
        n=n%998244353
        mul*=n**m%998244353
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))

Submission status: Time Limit Exceeded

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

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

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)
        n=n%998244353
        mul*=n**m
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))

Submission status: Time Limit Exceeded

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()
counted = []

for i in range(1,N):
    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: Time Limit Exceeded

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

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

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*=n**m
        mul=mul%998244353
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
ans = 1
#n1が0ではない、もしくは他に0がある
if D[0] != 0 or D.count(0) != 1:
  ans = 0
else:
  for i in range(1, max(D)):
    ans *= D.count(i) ** D.count(i + 1)
    if ans >= mod:
      ans %= mod
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import time
import random
import sys
input=sys.stdin.readline
#N=100000
N=int(input())
D=np.array(list(map(int,input().split())))
#D=np.concatenate([np.array([0]),np.array([random.randint(1,1000) for _ in range(99999)])])

#print(np.count_nonzero(D==0))
if D[0]!=0 or np.count_nonzero(D==0)!=1:
    print(0)
    exit()

k=max(D)


ans=1
past=1
for i in range(1,k+1):
    n=np.count_nonzero(D==i)
    if n==0:
        print(0)
        #ptime=time.time()-start
        #print(ptime)
        exit()
    ans*=past**n
    ans%=998244353
    past=n

print(ans)
#ptime=time.time()-start
#print(ptime)

Submission status: Time Limit Exceeded

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 *= a.count(i-1)**a.count(i)
            output %= 998244353
    print(output)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import time
import random
start = time.time()
#N=100000
N=int(input())
D=np.array(list(map(int,input().split())))
#D=np.concatenate([np.array([0]),np.array([random.randint(1,1000) for _ in range(99999)])])

#print(np.count_nonzero(D==0))
if D[0]!=0 or np.count_nonzero(D==0)!=1:
    print(0)
    exit()

k=max(D)


ans=1
past=1
for i in range(1,k+1):
    n=np.count_nonzero(D==i)
    if n==0:
        print(0)
        #ptime=time.time()-start
        #print(ptime)
        exit()
    ans*=past**n
    ans%=998244353
    past=n

print(ans)
#ptime=time.time()-start
#print(ptime)

Submission status: Time Limit Exceeded

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

if D[0] != 0:
  print('0')
else:
  for i in range(1, max(D)):
    answer *= D.count(i) ** D.count(i + 1)
  print(answer % 998244353)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = list(map(int, input().split()))
df = D[0]
D.sort()
l = []

for i in range(N):
  l.append(D.count(i))

ans = 1  

for i in range(N):
  if l[i] != 0 and i != N:
      ans *= l[i] ** l[i+1]

if df != 0:
  print(0)
else:
  print(ans%998244353)

Submission status: Time Limit Exceeded

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)

#calc
result = 1
x = D.count(1)
D = D[x:]
for i in range(2,m+1):
    y = D.count(i)
    D = D[y:]
    if y!= 0:
        result = result * pow(x,y)
        x = y
print(result%998244353)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
Ds=list(map(int,input().split()))
 
if min(Ds)!=0 or Ds.count(0)!=1:
    print(0)
else:
    Ans=[0]*(max(Ds)+1)
    Ans[0]=1
    for i in range(1, max(Ds)+1):
        Ans[i]=(Ans[i-1]*(Ds.count(i-1)**Ds.count(i)))
 
    print(Ans[max(Ds)]%998244353)

Submission status: Time Limit Exceeded

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

mem = [-1 for i in range(n+1)]
def countd(y):
  if mem[y] == -1:
    mem[y] = d.count(y)
  return mem[y]

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

count = 1
for i in range(n-1):
  x = d[i+1]
  if x == 0:
    print(0)
    exit()
  if x == 1:
    continue
  count = count * countd(x-1)

print(count % 998244353)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(a) for a in input().split()]

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

k = max(A)

count = 1
result = 1
Re = 1
while True:

    Back = Re
    Re = A.count(count)

    result *= (Back ** Re) % 998244353
    result %= 998244353

    count += 1

    if result == 0:
        print(0)
        exit()

    if k < count:
        break

print(result)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
D = input().split()
D = [int(d) for d in D]
if(D[0] != 0):
    print(0)
else:
    D = sorted(D[1:])
    if(D[-1]==1):
        print(1)
    else:
        n = 0
        k = 0
        mul = 1
        k_count_before = 1
        while(n<len(D)):
            k += 1
            k_count = 0
            
            while(n<len(D) and D[n]==k):
                k_count += 1
                n += 1

            if(k_count==0):
                mul = 0
            else:

                mul *= k_count_before**k_count
                k_count_before = k_count

        print(mul%998244353)

Submission status: Time Limit Exceeded

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

m = max(D)

#calc
result = 1
x = D.count(1)
for i in range(2,m+1):
    y = D.count(i)
    if y!= 0:
        result = result * pow(x,y)
        x = y
print(result%998244353)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

Solution code:
import sys


def answerA():
    input = sys.stdin.readline
    N = int(input())

    print((N-1) // 2)


def answer():
    input = sys.stdin.readline
    N = int(input())
    distances = list(map(int, input().split()))

    maximum = max(distances)
    counts = []

    for d in range(maximum + 1):
        counts.append(distances.count(d))

    result = 1
    mod = 998244353

    if 0 in counts:
        result = 0
    elif 0 in distances[1:]:
        result = 0
    else:
        for d in distances[1:]:
            result *= counts[d-1]
            result %= mod

    print(result)


if __name__ == '__main__':
    answer()

Submission status: Time Limit Exceeded

Solution code:
import sys


def solve():
    MOD = 998244353
    N, *d_l = map(int, open(0).read().split())
    ans = 1
    
    if d_l[0] != 0:
        print(0)
        sys.exit()
    
    d_max = max(d_l)
    count_l = [d_l.count(1)]
    for i in range(2, d_max+1):
        count_l.append(d_l.count(i))
    
    for i in range(1, d_max):
        ans = ans * count_l[i-1] ** count_l[i]
    
    print(ans%MOD)


if __name__ == '__main__':
    solve()

Submission status: Time Limit Exceeded

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

mem = [-1 for i in range(n+1)]
def countd(y):
  if mem[y] == -1:
    mem[y] = len([a for a in d if a == y])
  return mem[y]

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

count = 1
for i in range(n-1):
  x = d[i+1]
  if x == 0:
    print(0)
    exit()
  if x == 1:
    continue
  count = count * countd(x-1)

print(count % 998244353)

Submission status: Time Limit Exceeded

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

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

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*=n**m
        n=m
    
    ans=mul%998244353

else:
    ans=0

print(int(ans))