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: Wrong Answer

Solution code:
BIGMOD = 998244353

def modPow(x,n):
    ans = 1
    while n >= 1:
        if bin( n & 1)==bin(1):
            ans = (ans*x) % BIGMOD
        x = x * x % BIGMOD
        n = n>>1
    return ans % BIGMOD

def modFact(N):
    fact = []
    fact.append(1) #0! = 1
    for i in range(1,N+1):
        a = fact[i-1]*i % BIGMOD
        fact.append(a)
    return fact

def modInvFact(fact):
    invFact = []
    for i in range(1,N+2):
        a = modPow(fact[i-1],BIGMOD-2)
        invFact.append(a)
    return invFact

def modPlus(a,b):
    return (a + b) % BIGMOD

def modMinus(a,b):
    if a-b >= 0:
        return (a - b) % BIGMOD
    else:
        return (a - b + BIGMOD)
    
def modMulti(a,b):
    return a * b % BIGMOD

# fact(n),invFact(r),invFact(n-r)
def nCr(fact,invFact,n,r):
    return (fact[n] * invFact[r] * invFact[n-r] ) %BIGMOD

def nPr(fact,invFact,n,r):
    return(fact[n] * invFact[r])%BIGMOD

def fact(num):
    ans = 1
    for i in range(1,num+1):
        ans = ans*i%BIGMOD
    return ans

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

S = list(set(D))
S.sort()
if S[0]==0:
    S.pop(0)
ans = 1
before = 0
before_i = 0
for i in S:
    if i == 1:
        before = D.count(i)
        before_i = i
        continue
    ans = ans*modPow(before,D.count(i))
    before = D.count(i)
    if i - before_i!=1:
        ans = 0
    before_i = i
if D[0] >0:
    print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
d = list(map(int, input().split()))
D = {i:[] for i in range(max(d)+1)}
if sum(d) == 0 and n != 1:
  print(0)
  exit()
for i in range(n):
  if i == 0 and d[i] != 0:
    print(0)
    exit()
  if i != 0 and d[i] == 0:
    continue
  D[d[i]] += [i]
ans = 1

for i in range(max(d)):
  ans *= ((len(D[i]) ** len(D[i+1])) % 998244353)
  ans %= 998244353

print(ans % 998244353)

Submission status: Wrong Answer

Solution code:
import sys

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

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

#D.sort(reverse=True)


ans = 1
for d in D:
    if d == 0:
        continue

    elif d != 1:
        ans = (ans * d) % 998244353

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
N=int(input())
D=np.array(list(map(int,input().split())))



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)
        exit()
    ans*=past**n
    past=n

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from collections import Counter 
import sys

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

mod = 998244353

D.sort()
d = list(Counter(D).items()) 

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

ans = 1
for i in range(1,len(d)):
    for _ in range(d[i][1]):
        ans = ((ans * i) % mod)
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
d = list(map(int,input().split()))
li = []
d.sort()
cnt = 0
ans = 1
if d[0] != 0:
  print("0")
elif d[1] == 0:
  print("0")
else:
  for i in range(1,n-1):
    if d[i] == d[i+1]:
      cnt += 1
    else:
      cnt += 1
      li.append(cnt)
      cnt = 0
    if i == n-2:
      if d[n-2] == d[n-1]:
        cnt += 1
        li.append(cnt)
      else:
        li.append(1)
  for i in range(len(li)-1):
    ans = ans*((li[i]**li[i+1])%998244353)
  print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np


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

D_list = list(set(D))
D_count = []
for d_ in D_list:
    D_count.append(D.count(d_))
if (D_list[-1] != len(D_list) - 1) | (D_count[0] != 1):
    print(0)
else:
    ans = 1
    for d1, d2 in zip(D_count[:-1], D_count[1:]):
        ans *= d1**d2
    print(ans % 998244353)

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np


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_np = d_l[1:]
    count_l = [d_np.count(1)]
    for i in range(2, max(d_np)+1):
        if count_l[i-2] == 0:
            print(sys.exit())
        count_l.append(d_np.count(i))
    
    for i in range(1, max(d_np)):
        ans = ans * count_l[i-1] ** count_l[i]
    
    print(ans%MOD)


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int,input().split()))
d = { }
for i in range(N+1):
    d[i] = 0
for i in range(N):
    x = D[i]
    d[x] += 1
p = 0
if D[0] == 0:
    p = 1
q = 0
if d[0] == 1:
    q = 1
M = max(D)
r = 1
for i in range(1,M):
    x = d[i]
    y = d[i+1]
    r = r*(x**y)
ans = p*q*r
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit

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

c = {}
for i in D:
    if i in c:
        c[i] += 1
    else:
        c[i] = 1

d = list(c.keys())
d.sort()
if c[d[0]] != 1:
    print(0)
    exit()

result = 1
for i in range(d[0] + 1, N + 1):
    if i not in c:
        if max(d) == i - 1:
            break
        else:
            print(0)
            exit()
    result = result * (c[i - 1] ** c[i]) % 998244353
print(result)

Submission status: Wrong Answer

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

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

count = 1
dmax = max(d)
num_array = [0] * (dmax+1)

for i in range(len(d)):
    num_array[d[i]] += 1
    
for i in range(1, dmax+1):
    count *= i ** num_array[i]
print(count)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import Counter
N = int(stdin.readline().rstrip())
D = [int(x) for x in stdin.readline().rstrip().split()]
mod = 998244353
ans = 1
if len(set(D))==1:
    print(0)
else:
    tmp = 1
    cnt = Counter(D)
    #print(cnt)
    for i in range(1,max(D)+1):
        #print(cnt[i],tmp)
        c = tmp**cnt[i]
        ans *= c
        tmp = cnt[i]
    print(ans % mod)

Submission status: Wrong Answer

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

c = 0
if D[0] != 1:
    c += 1
    for i in range(1,max(D)):
        a = D.count(i)
        b = D.count(i+1)
        c *= a ** b
print(c)

Submission status: Wrong Answer

Solution code:
def input():
    import sys
    return sys.stdin.readline().strip()


def main():
    d = [input() for _ in range(2)]
    n = d[0]
    d = list(map(int, d[1].split(' ')))
    mod = 998244353
    if d.count(0) != 1:
        print(0)
    else:
        count = 1
        for i in d[1:]:
            count = ((count % mod) * (i % mod)) % mod
        print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
d = [int(i) for i in input().split(" ")]
if d[0]!=0:
    print(0)
else:
    te = [1] + [0 for i in range(max(d))]
    co = [0 for i in range(max(d)+1)]
    for i in d:
        co[i] += 1
    if 0 in co[1:]:
        print(0)
    elif co[0] != 1:
        print(0)
    elif len(co)==2:
        print(1)
    else:
        for i in range(1,len(co)):
            te[i] = te[i-1]*(co[i-1]**co[i])
        print(te[-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1

def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


class Mod:
    def __init__(self, m):
        self.m = m

    def add(self, a, b):
        return (a + b) % self.m

    def sub(self, a, b):
        return (a - b) % self.m

    def mul(self, a, b):
        return ((a % self.m) * (b % self.m)) % self.m

    def div(self, a, b):
        return self.mul(a, pow(b, self.m-2, self.m))

    def pow(self, a, b):
        return pow(a, b, self.m)


@mt
def slv(N, D):
    M = Mod(998244353)
    if D[0] != 0:
        return 0
    D.sort()
    C = Counter(D)
    ans = 1
    for i in range(1, max(D)+1):
        if C[i-1] == 0:
            return 0
        ans = M.mul(ans, M.pow(C[i-1], C[i]))
    return ans


def main():
    N = read_int()
    D = read_int_n()
    print(slv(N, D))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
MOD = 998244353

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


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

ans = 1
cnt = 1
precnt = 1

if Orig[0] != 0:
    ans = 0
if N > 1 and D[1] == 0:
    ans = 0

for i in range(N-1):
    if D[i] == D[i+1]:
        cnt += 1
    elif D[i] + 1 != D[i+1]:
        ans = 0
        break
    else:
        ans *= beki(precnt, cnt) % MOD
        precnt = cnt
        cnt = 1
        if (i == N-1):
            break
else:
    ans *= beki(precnt, cnt) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
MOD = 998244353
N = int(input())
D = list(map(int, input().split()))

lens = [0] * N

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

total = 1
prev_l = 0
for i, l in enumerate(lens):
    if i == 0:
        prev_l = l
        if l != 1:
            total = 0
            break
        continue
    total *= ((prev_l**l)%MOD)
    prev_l = l

print(total)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
Dlist = Counter(list(map(int, input().split())))
num = 1
mae = 1
for key, value in sorted(Dlist.items()):
    num *= (mae ** value)
    mae = value
print(num)

Submission status: Wrong Answer

Solution code:
import sys

pre_strk = 1
now_strk = 1
ans = 1
n = int(input())
d_list = list(map(int,input().split()))
d_list.sort()
# print(d_list)
if d_list[0] != 0:
    print(0)
    sys.exit()
# if d_list[1] == 0:
#     print(0)
#     sys.exit()

for i in range(1,n):
    if d_list[i] == d_list[i-1]:
        now_strk += 1
    elif d_list[i] != d_list[i-1] and (d_list[i-1] != 1 or d_list[i-1] != 0):
        ans *= pre_strk ** now_strk
        pre_strk = now_strk
        now_strk = 1
        # print(ans)
        ans = ans % 998244353
    else:
        pre_strk = now_strk
        now_strk = 1
ans *= pre_strk ** now_strk
print(ans%998244353)

Submission status: Wrong Answer

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

Ans = 0
number = 1
n_count = 0
b_count = 1

newD = sorted(D)

if newD[0] != 0 or newD[1] != 1:
  print(Ans)
  sys.exit()
else:
  
  for i in range(1,N):
    if newD[i] == number:
      n_count = n_count + 1
    elif newD[i] == number + 1:

      if number == 1:
        Ans = 1
        #print(Ans)
      else:
        Ans = Ans * (b_count ** n_count)
        Ans = Ans % 998244353
        #print(Ans)
        
      number = number + 1
      b_count = n_count
      n_count = 1
    else:
      Ans = 0
      print(Ans)
      sys.exit()

  Ans = Ans * (b_count ** n_count)
  Ans = Ans % 998244353
  print(Ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
if a[0] != 0:
  print(0)
else:
  def max(x):
    b = 0
    for i in range(len(x)):
      if a[i] > b:
        b = a[i]
    return b
  count = [0 for i in range(max(a)+1)]
  for i in range(n):
    count[a[i]] += 1
  can = True
  for i in range(len(count)):
    if count[i] == 0:
      can = False
  if can == False:
    print(0)
  else:
    c = 1
    for i in range(1, len(count)-1):
      c = c * (count[i] ** count[i+1])
    print(c%998244353)

Submission status: Wrong Answer

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

c = Counter(D)
max_D = max(D)
if c[0]!=1:
  print(0)
  
elif max_D<=1:
  print(1)
  
else:
  answer=1
  for i in range(2,max_D+1):
    c_i2 = c[i]
    c_i1=c[i-1]
    answer *= c_i1**c_i2
    
  print(answer%998244353)

Submission status: Wrong Answer

Solution code:
from collections import Counter

n=int(input())
a=sorted([int(i) for i in input().split()])
c=Counter(a)
ans=1
if c[0]!=1:
    print(0)
    quit()

for i in range(n-1):
    if a[i+1]-a[i]>1:
        print(0)
        quit()

for i in range(n-1):
    ans *= (c[i]**c[i+1])
    ans %= 998244353

print(ans)

Submission status: Wrong Answer

Solution code:
import collections

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

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

cnt = collections.Counter(A)

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

res = 1
for v in cnt.values():
  res *= v
print(res)

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    l = {}
    for i in input().split():
        i = int(i)
        if i in l:
            l[i] += 1
        else:
            l[i] = 1
    
    count = 1
    m = max(l)
    if 0 not in l:
        print(0)
        exit()
    
    for i in range(1,m+1):
        if i in l:
            count *= l[i-1] ** l[i] % 998244353
        else:
            print(0)
            exit()
    
    print(count%998244353)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

ans = 1
count = {}
for i in range(N):
  if i == 0 and D[i] != 0:
    print(0)
    exit()
  if i > 0:
    if D[i]-1 in count:
      ans *= count[D[i]-1]
      ans %= MOD
    else:
      print(0)
      exit()
  if D[i] in count:
    count[D[i]] += 1
  else:
    count[D[i]] = 1
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
ls = list(map(int, input().split(' ')))
ans = 1
if ls[0] != 0:
    ans = 0
m = max(set(ls))
if m > n:
    ans = 0
# d = {i: ls.count(i) for i in range(m+1)}
d = {}
for l in ls:
    d[l] = d.get(l, 0) + 1
if ans != 0:
    for i in range(1, m+1):
        if d.get(i, 0) == 0:
            ans = 0
            break
        ans = (ans * (d[i-1] ** d[i])) % 998244353
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

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

c = collections.Counter(D)
c = dict(c)

ans = 1
tmp = 1
for i in range(mD+1):
    if not(i in c.keys()):
        ans = 0
        break
    elif i == 0:
        if c[0] > 1:
            ans = 0
            break
    elif i == 1:
        tmp = c[i]
    else:
        ans = (ans * tmp**c[i])%MOD
        tmp = c[i]

if N == 1:
    if D[0] == 0:
        print(1)
    else:
        print(0)
else:
    print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

N = int(input())
D = [int(x) for x in input().split()]
D_counter = Counter(D)
if D[0] != 0:
    print(0)
    sys.exit()

ans = 1
for i in range(1, N):
    if D_counter[D[i] - 1] == 0:
        print(0)
        sys.exit()
    elif D_counter[D[i] - 1] != 1:
        ans = (ans * D[i]) % 998244353
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
D = list(map(int, input().split()))
ans = 1
counter = Counter(D)
pre_c = 1
for i, (n, c) in enumerate(sorted(counter.items())):
    if i != n:
        ans = 0
        break
    elif n == 0:
        continue
    ans *= n**c
print(ans)

Submission status: Wrong Answer

Solution code:
n= int(input())
d= list(map(int, input().split()))
y=(d[0]!=0)
d.sort()
x=[0]*(d[n-1]+1)
k=0
ans=False
for i in range(n):
    if d[i]==k:
        x[k]+=1
    elif d[i]==k+1:
        k+=1
        x[k]+=1
    else:
        ans=True
    #print(ans)
#print(d)
#print(x)
if ans or x[0]>=2 or y:
    print(0)
else:
    a=1
    for i in range(d[n-1]):
        a*=(x[i]**x[i+1])%998244353
    print(a)

Submission status: Wrong Answer

Solution code:
from sys import stdin
from collections import Counter
N = int(stdin.readline().rstrip())
D = [int(x) for x in stdin.readline().rstrip().split()]
mod = 998244353
ans = 1
if len(set(D))==1:
    print(0)
else:
    tmp = 1
    cnt = Counter(D)
    #print(cnt)
    for i in range(1,max(D)+1):
        #print(cnt[i],tmp)
        c = tmp**cnt[i]
        ans *= c
        tmp = cnt[i]
        ans = ans % mod
print(ans % mod)

Submission status: Wrong Answer

Solution code:
def input():
    import sys
    return sys.stdin.readline().strip()


def main():
    d = [input() for _ in range(2)]
    n = d[0]
    d = list(map(int, d[1].split(' ')))
    mod = 998244353
    if d.count(0) != 1:
        print(0)
    else:
        count = 1
        for i in d[1:]:
            count = (count % mod * i % mod) % mod
        print(count)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

ans=1

a=[0]*(n+1)

for i in d:
  a[i] += 1

for j in range(n):
  if j!=0 and a[j] == 0:
    for k in range(j+1,n):
      if a[k] != 0:
        print(0)
        exit()
    break
  else:
    ans*=(a[j]**a[j+1])
    ans%=998244353

print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
d = [0]*n
for i in input().split():
  d[int(i)]+=1
if d[0]!=1:
  print(0)
else:
  mod = 998244353
  ans=1
  for i in range(n-2):
    ans*=(d[i]**d[i+1])%mod
  print(ans%mod)

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
d = list(map(int, input().split()))
if d[0] != 0:
  print(0)
else:
  del d[0]
  c = collections.Counter(d)
  ll = len(c)
  ans = 1
  for i in range(2, ll+1):
    if c[i] == 0:
      print(0)
      break
    else:
      ans *= c[i-1]**c[i]
  print(ans%998244353)

Submission status: Wrong Answer

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

m = {}
for item in d:
    if item in m:
        m[item] += 1
    else:
        m[item] = 1

prev = 1
if (0 in m) and (m[0] == 1):
    s = 1
    visited = 1
    for i in range(1, 10 ** 5 + 1):
        if not i in m:
            break
        v = m[i]
        visited += v

        s *= (prev ** v)
        prev = v
    
    if visited != n:
        s = 0
else:
    s = 0

print(s % 998244353)

Submission status: Wrong Answer

Solution code:
from collections import Counter
mod=998244353
N=int(input())
D=list(map(int,input().split()))
if D[0]!=0:
  print(0)
else:
  tmp=Counter(D)
  ans=1
  for i in tmp.keys():
    if i==0:
      continue
    ans*=tmp[i-1]**tmp[i]
    ans=ans%mod
  print(ans)

Submission status: Wrong Answer

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

ans = 1
# 距離:頂点に対応する辞書
if D[0] != 0:
    ans = 0
distance_dict = {0:1}
for i in D[1:]:
    if not i in distance_dict:
        distance_dict[i] = 1
    else:
        distance_dict[i] += 1

#dist_list = list((distance_dict.keys())) # 0~N-1 一つでもとんでったらアウト。距離がない
#print(distance_dict)

for i in range(1,max(distance_dict.keys())+1):
    if not i in distance_dict:
        ans = 0
        print(0)
        exit()
    else:
        ans *= (distance_dict[i-1]**distance_dict[i])%BIGNUM # どこにつながるかの候補とつながる点の数
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import collections
import sys


def solve():
    input = sys.stdin.readline
    mod = 998244353
    n = int(input().rstrip('\n'))
    d = list(map(int, input().rstrip('\n').split()))
    mn = 0
    dic = collections.defaultdict(list)
    for i in range(n):
        dic[d[i]] += [i+1]
        mn = max(mn, d[i])
    if len(dic[0]) == 1:
        ln = 1
        t = 1
        for i in range(1, mn+1):
            if len(dic[i]) != 0:
                t *= ln ** len(dic[i])
                t %= mod
                ln = len(dic[i])
            else:
                print(0)
                exit()
        print(t)
    else:
        print(0)
        exit()


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
MOD = 998244353

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


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

ans = 1
cnt = 1
precnt = 1

if Orig[0] != 0:
    ans = 0
if N > 1 and D[-1] == 0:
    ans = 0

for i in range(N-1):
    if D[i] == D[i+1]:
        cnt += 1
    elif D[i] + 1 != D[i+1]:
        ans = 0
        break
    else:
        ans *= beki(precnt, cnt) % MOD
        precnt = cnt
        cnt = 1
        if (i == N-1):
            break
else:
    ans *= beki(precnt, cnt) % MOD
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

def read():
    N = int(input().strip())
    D = list(map(int, input().strip().split()))
    return N, D

def argsort(x):
    pairs = [(i, x[i]) for i in range(len(x))]
    pairs.sort(key=lambda a: a[1])
    return [a[0] for a in pairs]


def solve(N, D, P=998244353):
    D = [-1] + D
    if D[1] > 0:
        return 0
    count = Counter(D)
    count_max = max(count.keys())
    result = 1
    for i in range(1, count_max + 1):
        if count[i] == 0:
            return 0
        n_childs = count[i]
        n_parents = count[i-1]
        ptn = n_parents ** n_childs
        result *= ptn
        result %= P
    return result

if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Submission status: Wrong Answer

Solution code:
MOD = 998244353

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

stage = [0]*n
maxstage = 0
for di in d:
    stage[di] +=1
    maxstage = max(maxstage, di)

if stage[0] != 1:
    print(0)
else:
    cnt = 1
    for i in range(1,maxstage+1):
        cnt = (cnt*stage[i-1]**stage[i])%MOD
           
    print(cnt)

Submission status: Wrong Answer

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


N = int(input())
D = list(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: Wrong Answer

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

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

ans = 1
for i in range(n):
    if i == 0:
        if a[i] != 1:
            ans *= 0
    else:
                for _ in range(a[i]):
                    ans *= a[i-1]
                    ans %= MOD
print(ans % MOD)

Submission status: Wrong Answer

Solution code:
N=int(input())
D=sorted(map(int,input().split()))
dp=[0]
x,r=0,1
m=998244353
if D[0]!=0:
    r=0
for d in D:
    if d==x:
        dp[x]+=1
    elif d==x+1:
        dp.append(1)
        x+=1
    else:
        r=0
    r=(r*dp[x-1])%m
print(r%m if dp[0]==1 else 0)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N = int(input())
D = list(map(int,input().split()))
if D[0] != 0:
    print(0)
    exit()
D = D[1:]
DD = Counter(D)
#print(DD)
MOD = 998244353
ans = 1
if len(DD) == 1:
    print(1)
    exit()
for i in range(2,len(DD)+1):
    ans *= (DD[i-1] ** DD[i])
    ans %= MOD
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
N = int(input())
D = list(map(int, input().split()))
D.sort()
cnt = collections.Counter(D)
if cnt[0] == 0:
    ans = 0
else:
    ans = 1
    i = 1
    while i <= max(D):
        ans *= i ** cnt[i]
        i += 1
print(ans%998244353)

Submission status: Wrong Answer

Solution code:
n=int(input())
D=list(map(int,input().split()))
T=[[] for _ in range(max(D)+1)]
for i in range(n):
    T[D[i]].append(i+1)
if len(T[0])!=1:
    print(0)
    exit()
a=1
for j in range(1,len(T)):
    if T[j]==[]:
        print(0)
        exit()
    else:
        a*=len(T[j-1])**len(T[j])
        a%=998244353
print(a)

Submission status: Wrong Answer

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(1, maximum + 1):
        counts.append(distances.count(d))

    result = 1
    mod = 998244353

    if distances[0] != 0:
        result = 0
    elif 0 in counts:
        result = 0
    elif 0 in distances[1:]:
        result = 0
    else:
        for i, count in enumerate(counts):
            result *= counts[i-1]**count
            result %= mod

    print(result)


if __name__ == '__main__':
    answer()

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter
input = sys.stdin.readline

N = int(input().strip())
C = [0] * N
D = [int(x) for x in input().strip().split()]
maxD = max(D)
for d in D:
    C[d] += 1
ans = 1
c_ = 1
if D[0] != 0:
    ans = 0
else:
    for i, c in enumerate(C[1:], start=1):
        if i > maxD:
            break
        if c == 0:
            ans = 0
            break
        ans *= c_ ** c
        ans %= 998244353
        c_ = c

print(ans)

Submission status: Wrong Answer

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

l = []
for i in range(n):
    c = d.count(i)
    l.append(c)

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

if d[0] != 0:
    print(0)
else:
    print(ans%wari)

Submission status: Wrong Answer

Solution code:
n = int(input())
ls = list(map(int, input().split(' ')))
n = 100000
ls = [i%31 for i in range(n)]
ans = 1
if ls[0] != 0:
    ans = 0
m = max(set(ls))
if m > n:
    ans = 0
# d = {i: ls.count(i) for i in range(m+1)}
d = {}
for l in ls:
    d[l] = d.get(l, 0) + 1
if ans != 0:
    for i in range(1, m+1):
        if d[i] == 0:
            ans = 0
        ans = (ans * (d[i-1] ** d[i])) % 998244353
print(ans)

Submission status: Wrong Answer

Solution code:
from sys import exit

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

c = {}
for i in D:
    if i in c:
        c[i] += 1
    else:
        c[i] = 1

d = list(c.keys())
d.sort()
if c[d[0]] != 1:
    print(0)
    exit()

result = 1
for i in range(d[0] + 1, N):
    if i not in c:
        if max(d) == i - 1:
            break
        else:
            print(0)
            exit()
    result = result * (c[i - 1] ** c[i]) % 998244353
print(result)

Submission status: Wrong Answer

Solution code:
from collections import Counter as co
n=int(input())
l=list(map(list,co(map(int,input().split())).items()))
if l[0][-1]>1:print(0);exit()
ans=1;pre=1
for i,(n,s) in enumerate(l):
  if i!=n:print(0);exit()
  ans*=(pre**s)
  pre=s
print(ans) if len(l)>=3 else print(1)

Submission status: Wrong Answer

Solution code:
n=int(input())
d=list(map(int,input().split()))
num=[0 for i in range(n)]
ans=1
for i in d:
  num[i]+=1
if d[0]>=1:
  print(0)
else:
  for i in range(1,n-1):
      ans*=num[i]**num[i+1]
      while ans>998244353:
        ans=ans%998244353
for i in range(1,n):
  if d[i]==0 and ans>0:
    ans=0
print(ans)

Submission status: Wrong Answer

Solution code:
def listConvert(baseList):
  maxNum = max(baseList)
  newList = [0] * (maxNum + 1)
  for a in baseList:
    newList[a] += 1
  return newList

n = int(input())
baseList = list(map(int, input().split()))
isZero = False

if baseList[0] != 0:
  print(0)
  isZero = True

newList = listConvert(baseList)
if newList[0] > 1:
  print(0)
  isZero = True
if newList.count(0) > 0:
  print(0)
  isZero = True

result = 0
if not isZero:
  result = 1
  preA = 1
  mod = 998244353
  for a in newList:
    result = (result * pow(preA, a, mod)) % mod
    preA = a

print(result)

Submission status: Wrong Answer

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

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

if D[0] > 0:
    print(0)
    exit(0)
else:
    num = [0]*max(D)
    for i in range(1, N):
        if D[i] > 0:
            num[D[i]-1] += 1

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

    print(ans % mod)

Submission status: Wrong Answer

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

ans = 1
# 距離:頂点に対応する辞書
if D[0] != 0:
    ans = 0
distance_dict = {0:1}
for i in D[1:]:
    if not i in distance_dict:
        distance_dict[i] = 1
    else:
        distance_dict[i] += 1

#dist_list = list((distance_dict.keys())) # 0~N-1 一つでもとんでったらアウト。距離がない
#print(distance_dict)

for i in range(1,max(distance_dict.keys())+1):
    if not i in distance_dict:
        ans = 0
        print(0)
        exit()
    else:
        ans *= distance_dict[i-1]**distance_dict[i] # どこにつながるかの候補とつながる点の数
print(ans%BIGNUM)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

d.sort()

if d[0] == 0 and d[1] != 0:
    pass
else:
    print(0)
    exit()

set_d = set(d)
list_d = list(set_d)
dict_d = Counter(d)

ans = 1
cnt = 1
temp = 0
check = 1

for i in list_d:
    if i == 0:
        continue
    if i != temp + 1:
        print(0)
        exit()
        
    temp = i
    cnt = dict_d[i]
    ans = ans * check**cnt
    check = cnt
    
print(ans%998244353)