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:
import collections
N  = int (input())
nums = list(map(int,input().split(" ")))
MOD = 998244353
di = collections.defaultdict(lambda :0)
for num in nums:
    di[num] += 1


ans = 1 if nums[0] == 0 else 1
ans = 0 if 0 in nums[1:]  else 1
bottom = min(nums)
top= max(nums)
for d in range(1,top + 1):
    ans *= pow(di[d - 1] ,di[d ],MOD)


print(ans)

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
D = LIST()

mod = 998244353
c = Counter(D)
v = max(D)

if D[0] != 0:
    print(0)
else:
    ans = 1
    for i in range(1, v + 1):
        ans *= pow(c[i-1], c[i], mod)
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

M = 998244353
N = int(input())
D = list(map(int, input().split()))
assert len(D) == N
dcnt = Counter(D)

def solve():
    if D[0] != 0:
        return 0
    D.sort()
    if len(D) > 1 and D[1] != 1:
        return 0
    i = 1
    res = 1
    leaf = 1
    while i < N:
        k = D[i]
        c = dcnt[k] % M
        res *= pow(leaf, c, M)
        leaf = c
        i += c
    return res

if __name__ == "__main__":
    print(solve())

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
ans = 1
A = [0]*(max(D)+1)
for i in range(N):
  A[D[i]] += 1
#if D[0] != 0 or not all(D[1:]) or not all(A):
#  print(0)
#  exit()
if D[0] != 0:
  print(0)
  exit()
for i in range(1, max(D)+1):
  if A[i] == 0:
    print(0)
    exit()
  ans = ans * pow(A[i-1], A[i], mod) % mod
print(ans % mod)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split()))
mod = 998244353
if D[0] != 0:
  print(0)
  exit()
ans = 1
A = [0]*(max(D)+1)
# for i in range(N):
#  A[D[i]] += 1

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

for i in range(max(D)+1):
  if A[i] == 0:
    print(0)
    exit()
  if i > 0:
    ans *= pow(A[i-1], A[i], mod) % mod
print(ans % mod)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

mod = 998244353

from collections import Counter, deque

def solve(N,D):
    if D[0]!=0:
        return 0
    c = Counter(D)
    # if c[0]>1:
        # return 0
    ans = 1
    m = max(D)
    for i in range(1,m):
        # if c[i]==0:
            # ans = 0
            # continue
        ans *= pow(c[i],c[i+1],mod)
        ans %= mod
    return ans

print(solve(N,D))

Submission status: Wrong Answer

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

mod = 998244353

from collections import Counter, deque, defaultdict

def solve(N,D):
    if D[0]!=0:
        return 0
    c = Counter(D)
    ans = 1
    m = max(D)
    for i in range(1,m):
        if c[i]==0:
            ans = 0
            continue
        ans *= pow(c[i],c[i+1],mod)
    return ans

print(solve(N,D))

Submission status: Wrong Answer

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

mod = 998244353

from collections import Counter, deque, defaultdict

def solve(N,D):
    if D[0]!=0:
        return 0
    c = Counter(D)
    ans = 1
    for i in range(1,N-1):
        ans *= pow(c[i],c[i+1],mod)
    return ans

print(solve(N,D))

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

if D[0] != 0:
    print(0)
else:
    c = Counter(D)
    c = sorted(c.items(), key=lambda x: x[0])
    ans = 1
    prev = 1
    dist = [False] * (N + 1)
    dist[0] = True
    for k, v in c:
        if k == 0:
            if v > 1:
                print(0)
                break
        else:
            if dist[k - 1] is not True:
                print(0)
                break
            dist[k] = True
            ans *= prev ** v
            ans %= MOD
            prev = v

    print(ans % MOD)

Submission status: Wrong Answer

Solution code:
from collections import Counter
N,*D = map(int, open(0).read().split())
MOD = 998244353
c = Counter(D)
cnt = 1
M = max(c.keys())
ans = 1
while cnt<=M:
  ans *= c[cnt-1]**c[cnt]
  ans %= MOD
  cnt += 1
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

if D[0] != 0:
    print(0)
else:
    c = Counter(D)
    c = sorted(c.items(), key=lambda x: x[0])
    ans = 1
    prev = 1
    for k, v in c:
        if k == 0:
            if v > 1:
                print(0)
                break
        else:
            ans *= (prev ** v) % MOD
            prev = v

    print(ans % MOD)

Submission status: Wrong Answer

Solution code:
from collections import Counter

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

if D[0] != 0:
    print(0)
else:
    c = Counter(D)
    c = sorted(c.items(), key=lambda x: x[0])
    ans = 1
    prev = 1
    for k, v in c:
        if k == 0:
            if v > 1:
                print(0)
                break
        else:
            ans *= prev ** v
            prev = v

    print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


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

    if D[0] != 0:
        print(0)
    else:
        ans = 1
        prev_cnt = 1
        cnt = 0
        v = 0
        for i in range(N):
            if D[i] == v:
                cnt += 1
            elif v + 1 == D[i]:
                v += 1
                ans *= (prev_cnt ** cnt)
                prev_cnt = cnt
                cnt = 1
            else:
                ans = 0
                break
        ans *= (prev_cnt ** cnt)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.readline


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

    if D[0] != 0:
        print(0)
    else:
        ans = 1
        for i in range(1, N):
            cnt = D[:i].count(D[i] - 1)
            if cnt == 0:
                ans = 0
                break
            ans *= cnt
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import collections

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

mod = 998244353

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

m = max(d)

cnt = collections.Counter(d)
if cnt[0] != 1:
    exit()

ans = 1

for i in range(1, m + 1):
    if cnt[i] == 0:
        print(0)
        exit()
    ans *= pow(cnt[i - 1], cnt[i], mod)
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
n=int(input())
A=list(map(int,input().split()))
mod=998244353
C=[0]*n
for i in range(n):
  C[A[i]]+=1
ans=1
for i in range(n-1):
  ans=ans*pow(C[i],C[i+1],mod)%mod
print(ans)

Submission status: Wrong Answer

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

C = collections.Counter(A)

if list(sorted(set(A))) == [0, 1] or len(set(A)) == 1:
    print(0)
else:
    t = 1
    for i in C:
        if i == 0 or i == 1:
            continue
        else:
            t *= i**C[i]
    print(t)

Submission status: Wrong Answer

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

count = [0 for i in range(N)]
for i in range(N):
    count[D[i]] += 1
max_dist = max(D)
ans = count[0]

for i in range(1, max_dist + 1):
    ans *= (count[i - 1] ** count[i]) % mod
    ans %= mod

if count[0] > 1 and D[0] == 0:
    ans = 0

print(ans % 998244353)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter

n = int(readline())
d = tuple(map(int, readline().split()))
counter = Counter(d)
max_d = max(d)
mod = 998244353
ans = 1
if d[0] != 0 or counter[0] > 1:
    print(0)
    exit()
for i in range(1, max_d + 1):
    if counter[i] == 0:
        print(0)
        exit()
    ans *= pow(counter[i - 1], counter[i], mod) % mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter

n = int(readline())
d = tuple(map(int, readline().split()))
counter = Counter(d)
max_d = max(counter)
mod = 998244353
ans = 1
if d[0] != 0 or counter[0] > 1:
    print(0)
    exit()
for i in range(1, max_d + 1):
    if counter[i] == 0:
        print(0)
        exit()
    ans *= pow(counter[i - 1], counter[i], mod) % mod
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from collections import Counter

n = int(readline())
d = tuple(map(int, readline().split()))
counter = list(Counter(d).items())
counter.sort()
mod = 998244353
ans = 1
check = 0
next = 1
for v, c in counter:
    if v != check:
        print(0)
        exit()
    check += 1
    ans *= pow(next, c, mod) % mod
    next = c
print(ans)

Submission status: Wrong Answer

Solution code:
import collections
N=int(input())
D=list(map(int,input().split()))
l=[]
c=collections.Counter(D)
values=list(c.values())	#aのCollectionのvalue値のリスト(n_1こ、n_2こ…)
key=list(c.keys())	#先のvalue値に相当する要素のリスト(要素1,要素2,…)
p=998244353
cnt=1
for i in range(len(key)):
    l.append([key[i],values[i]])#lは[要素i,n_i]の情報を詰めたmatrix
#print(l)
if (c[0]==1)and(D[0]==0):
    for i in range(N-1):
        cnt=cnt*pow(c[i],c[i+1],p)
    print(cnt)
else:
    print("0")

Submission status: Wrong Answer

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

N = int(input().split()[0])
d_list = list(map(int, input().split()))
counter = collections.Counter(d_list)
max_d = max(counter.keys())
w = 0
mod = 998244353

if d_list[0] != 0:
    w = 0
else:
    for d in range(1, max_d + 1):
        if d not in counter.keys():
            w = 0
            break
        if d == 1:
            w += 1
            continue
        x = counter[d - 1] ** counter[d]  # D=iとなるルートのパターン数
        # w *= x
        w = (w % mod) * x
w = w % mod
ans = w
print(ans)

Submission status: Wrong Answer

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

N = int(input().split()[0])
d_list = list(map(int, input().split()))
counter = collections.Counter(d_list)
max_d = max(counter.keys())
w = 0
mod = 998244353

if d_list[0] != 0:
    w = 0
else:
    for d in range(1, max_d + 1):
        if d not in counter.keys():
            w = 0
            break
        if d == 1:
            w += 1
            # p_dict[i] = counter[i]  # D = 1となるルート数
            continue
        x = counter[d - 1] ** counter[d]  # D=iとなるルートのパターン数
        w *= x
w = w % mod
ans = w
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split(" ")))
count = [0 for i in range(N)]
for i in range(N):
    count[D[i]] += 1
max_dist = max(D)
ans = count[0]

for i in range(1, max_dist + 1):
    ans *= count[i - 1] ** count[i]

if count[0] > 1 and D[0] == 0:
    ans = 0
print(ans % 998244353)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split(" ")))
count = [0 for i in range(N)]
for i in range(N):
    count[D[i]] += 1
max_dist = max(D)
ans = count[0]

for i in range(1, max_dist + 1):
    ans *= count[i - 1] ** count[i]

if min(D) > 0 or count[0] > 1:
    ans = 0
print(ans % 998244353)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split(" ")))
count = [0 for i in range(N)]
for i in range(N):
    count[D[i]] += 1
max_dist = max(D)
ans = count[0]

for i in range(1, max_dist + 1):
    ans *= count[i - 1] ** count[i]
    ans %= 998244353

if min(D) > 0 or count[0] > 1:
    ans = 0
print(ans)

Submission status: Wrong Answer

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

branchs = [0] * N
MOD = 998244353

for d in D_list:
    branchs[d] += 1

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

cnt = 1
for v1,v2 in zip(branchs, branchs[1:]):
    cnt *= v1**v2 % MOD
    
print(cnt%MOD)

Submission status: Wrong Answer

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

branchs = [0] * N
MOD = 998244353

for i,d in enumerate(D_list):
    branchs[d] += 1
    
if branchs[0] != 1:
    print(0)
    exit()

cnt = 1
bef = branchs[0]
for i, v in enumerate(branchs[1:]):
    cnt *= bef**v % MOD
    bef = v
    
print(cnt%MOD)

Submission status: Wrong Answer

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

branchs = [0] * N
MOD = 998244353

for i,d in enumerate(D_list):
    branchs[d] += 1
cnt = 0
if branchs[0] == 0:
    print(0)
    exit()

cnt = 1
bef = branchs[0]
for i, v in enumerate(branchs[1:]):
    cnt *= bef**v % MOD
    bef = v
    
print(cnt%MOD)

Submission status: Wrong Answer

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

branchs = [[] for _ in range(10**5)]
d_max = 0
MOD = 998244353

for i,d in enumerate(D_list):
    branchs[d].append(i)
    d_max = max(d_max, d)
cnt = 0
if len(branchs[0]) == 1:
    cnt = 1
    bef = len(branchs[0])
    for i, l in enumerate(branchs[1:d_max+1]):
        if len(l) == 0:
            cnt = 0
            break
        cnt *= bef**len(l) % MOD
        bef = len(l)
    
print(cnt)

Submission status: Wrong Answer

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

branchs = [[] for _ in range(10**5)]
d_max = 0
MOD = 998244353

for i,d in enumerate(D_list):
    branchs[d].append(i)
    d_max = max(d_max, d)
cnt = 0
if len(branchs[0]) != 0:
    cnt = 1
    bef = len(branchs[0])
    for i, l in enumerate(branchs[1:d_max+1]):
        if len(l) == 0:
            cnt = 0
            break
        cnt *= bef**len(l) % MOD
        bef = len(l)
    
print(cnt)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)
v = max(D)

if c[0]!=1 or D[0]!=0:
    print(0)
else:
    ans = 1
    for i in range(1, v+1):
        ans *= (c[i-1]**c[i])
        ans %= mod

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)
v = max(D)

if c[0]!=1:
    print(0)
else:
    ans = 1
    for i in range(1, v+1):
        if c[i-1]==0 or c[i]==0:
            print(0)
            break
        ans *= (c[i-1]**c[i])
        ans %= mod
    else:
        print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)
v = max(D)

if c[0]!=1:
    print(0)
else:
    ans = 1
    for i in range(1, v+1):
        if c[i-1]==0 or c[i]==0:
            print(0)
            break
        ans *= (c[i-1]**c[i])
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)
v = max(D)

if c[0]!=1 or v>N-1:
    print(0)
else:
    ans = 1
    for i in range(1, v+1):
        ans *= (c[i-1]**c[i])
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)
v = max(D)

if c[0]>1:
    print(0)
else:
    ans = 1
    for i in range(1, v+1):
        ans *= (c[i-1]**c[i])
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)
v = max(D)

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)

keys = sorted(c.keys())

if N==1 and D[0]!=0:
    print(0)
else:
    ans = 1
    for i in range(1, keys[-1]+1):
        ans *= (c[i-1]**c[i])
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)

keys = sorted(c.keys())

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

Submission status: Wrong Answer

Solution code:
from collections import Counter

N = int(input())
*D, = map(int, input().split())
mod = 998244353
c = Counter(D)

keys = sorted(c.keys())
if keys[0]!=0:
    print(0)
else:
    size = len(keys)

    ans = 1
    for i in range(1, size):
        a, b = keys[i-1], keys[i]
        ans *= (c[a]**c[b])
        ans %= mod
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
from collections import Counter as co
f=list(map(int,input().split()))
l=co(sorted(f))
if f[0] or l[0]>=2:print(0)
else:
    ans=1
    a=1
    for i in l.values():
        ans*=a**i
        a=i
    print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
k=[0]*n
for i in l:k[i]+=1
if k[0]:
    ans=1
    a=1
    for i in k:
        ans*=a**i
        a=i
    print(ans)
else:print(0)

Submission status: Wrong Answer

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

maxd = max(D)

D.sort()

d = [0]*(maxd+1)

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

c = 1

if d[0]!=1:
    print(0)
    quit()

for i in range(1, maxd+1):
    c *= (d[i-1]**d[i])%998244353
    c %= 998244353
print(c)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split()))
M = [0]*N
for d in D: M[d]+=1

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

ans = 1
mod = 998244353
for i in range(1,N):
    if M[i-1]==0 and M[i]>0:
      exit()
    ans *= (M[i-1]**M[i])
    ans %= mod

print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
D = list(map(int, input().split()))
M = [0]*N
for d in D: M[d]+=1

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

ans = 1
mod = 998244353
for i in range(1,N):
    if M[i]==0:
      exit()
    ans *= (M[i-1]**M[i])
    ans %= mod

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
d = map(int, input().split())
dist = [0] * n
mod = 998244353
dp = [0] * n
if next(d) == 0:
    dp[0] = 1
    dist[0] += 1
for i in d:
    dist[i] += 1

for i in range(n-1):
    dp[i+1] = (dp[i] * pow(dist[i], dist[i + 1], mod)) % mod
print(max(dp))

Submission status: Wrong Answer

Solution code:
n = int(input())
d = map(int, input().split())
dist = [0] * n
mod = 998244353
dp = [0] * n
if next(d) == 0:
    dp[0] = 1
    dist[0] += 1
for i in d:
    dist[i] += 1

for i in range(n-1):
    dp[i+1] = (dp[i] * pow(dist[i], dist[i + 1], mod)) % mod
print(dp[-1])

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
mod=998244353
ans=1
memo=[0]*(max(a)+1)

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

if memo[0]!=1 or 0 not in a:
    print(0)
    exit()
    
before=1    
for num in memo:
    ans*=pow(before,num,mod)
    before=num
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
mod=998244353
ans=1
memo=[0]*(max(a)+1)

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

if memo[0]!=1:
    print(0)
    exit()
    
before=1    
for num in memo:
    ans*=pow(before,num,mod)
    before=num
print(ans%mod)

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
mod=998244353
ans=1
memo=[0]*(max(a)+1)

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

if memo[0]!=1:
    print(0)
    exit()
    
before=1    
for num in memo:
    ans*=pow(before,num,mod)
    before=num
print(ans)

Submission status: Wrong Answer

Solution code:
def main(): 
    n=int(input())
    a=list(map(int,input().split()))
    a=sorted(a)
    import math
    mod=998244353
    ans=0
    now_num=0
    cnt=1
    before=1
    
    flag=True
    for i in range(n):
        if (i==0 and a[i]!=0) or (i>0 and a[i]==0):
            flag=False
        if i==0:
            continue
        if a[i]==now_num:
            cnt+=1
        elif a[i]-now_num!=1:
            flag=False
        else:
            tmp=before*math.factorial(cnt)
            ans+=tmp
            now_num+=1
            before=tmp
            cnt=1
        ans%=mod
    else:
        tmp=before*math.factorial(cnt)
        ans+=tmp
    ans-=(a.count(1)+1)
    ans%=mod
    print(ans if flag==True else 0)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main(): 
    n=int(input())
    a=list(map(int,input().split()))
    a=sorted(a)
    import math
    mod=998244353
    ans=0
    now_num=0
    cnt=1
    before=1
    
    flag=True
    for i in range(n):
        if (i==0 and a[i]!=0) or (i>0 and a[i]==0):
            flag=False
        if i==0:
            continue
        if a[i]==now_num:
            cnt+=1
        elif a[i]-now_num!=1:
            flag=False
        else:
            tmp=before*math.factorial(cnt)
            ans+=tmp
            now_num+=1
            before=tmp
            cnt=1
        ans%=mod
    else:
        tmp=before*math.factorial(cnt)
        ans+=tmp
    ans%=mod
    print(ans-a.count(1)-1 if flag==True else 0)

if __name__=="__main__":
    main()

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a=sorted(a)
import math

ans=0
now_num=0
cnt=1
before=1

flag=True
for i in range(n):
    if (i==0 and a[i]!=0) or (i>0 and a[i]==0):
        flag=False
    if i==0:
        continue
    if a[i]==now_num:
        cnt+=1
    elif a[i]-now_num!=1:
        flag=False
    else:
        tmp=before*math.factorial(cnt)
        ans+=tmp
        now_num+=1
        before=tmp
        cnt=1
else:
    tmp=before*math.factorial(cnt)
    ans+=tmp
print(ans-a.count(1)-1 if flag==True else 0)

Submission status: Wrong Answer

Solution code:
from collections import Counter
inputs = open(0).readlines()

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

nbs = [0] * (max(D)+1)
for k, v in Counter(D).items():
    nbs[k] = v

if D[0] != 0 or nbs[0] != 1:
    print(0)
else:
    ans = 1
    for n0, n1 in zip(nbs, nbs[1:]):
        ans = ans * pow(n0, n1, mod) % mod
    print(ans)

Submission status: Wrong Answer

Solution code:
from collections import Counter
inputs = open(0).readlines()

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

nbs = [0] * (max(D)+1)
for k, v in Counter(D).items():
    nbs[k] = v

if nbs[0] == 1 and D[0] == 0:
    ans = 1
    for n0, n1 in zip(nbs, nbs[1:]):
        ans = ans * pow(n0, n1, mod) % mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

nbs = [0] * (max(D)+1)
for k, g in groupby(D):
    nbs[k] = len(list(g))

if nbs[0] == 1 and D[0] == 0:
    ans = 1
    for n0, n1 in zip(nbs, nbs[1:]):
        ans = ans * pow(n0, n1, mod) % mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

nbs = [0] * (max(D)+1)
for k, g in groupby(D):
    nbs[k] = len(list(g))

if nbs[0] == 1 and 0 not in nbs:
    ans = 1
    for n0, n1 in zip(nbs, nbs[1:]):
        ans = ans * pow(n0, n1, mod) % mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if nodes[0] == 1 and all(i == k for i, k in enumerate(keys)):
    ans = 1
    for n0, n1 in zip(nodes, nodes[1:]):
        ans = ans * pow(n0, n1, mod) % mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if nodes[0] == 1 and all(i == k for i, k in enumerate(keys)):
    ans = 1
    for n0, n1 in zip(nodes, nodes[1:]):
        ans *= pow(n0, n1, mod) % mod
    print(ans)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if nodes[0] == 1 and all(i == k for i, k in enumerate(keys)):
    patterns = [1]
    for n0, n1 in zip(nodes, nodes[1:]):
        patterns.append(patterns[-1] * pow(n0, n1, mod) % mod)
    print(patterns[-1])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if nodes[0] == 1 and all(i == k for i, k in enumerate(keys)):
    patterns = [1]
    for n0, n1 in zip(nodes, nodes[1:]):
        patterns.append(patterns[-1] * pow(n0, n1, mod) % mod)
    print(patterns[-1])
else:
    print(0)

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if not (keys[0] == 0 and nodes[0] == 1) or any(i != k for i, k in enumerate(keys)):
    print(0)
else:
    patterns = [1]
    for n0, n1 in zip(nodes, nodes[1:]):
        patterns.append(patterns[-1] * pow(n0, n1, mod) % mod)
    print(patterns[-1])

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if keys[0] != 0 or nodes[0] != 1 or any(i != k for i, k in enumerate(keys)):
    print(0)
else:
    patterns = [1]
    for n0, n1 in zip(nodes, nodes[1:]):
        patterns.append(patterns[-1] * pow(n0, n1, mod) % mod)
    print(patterns[-1])

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()

n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if nodes[0] != 1 or any(i != k for i, k in enumerate(keys)):
    print(0)
else:
    patterns = [1]
    for n0, n1 in zip(nodes, nodes[1:]):
        patterns.append(patterns[-1] * pow(n0, n1, mod) % mod)
    print(patterns[-1])

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys, nodes = [], [] 
for k, g in groupby(D):
    keys.append(k)
    nodes.append(len(list(g)))

if nodes[0] != 1 or all(i == k for i, k in enumerate(keys)):
    print(0)
else:
    patterns = [1]
    for n0, n1 in zip(nodes, nodes[1:]):
        patterns.append(patterns[-1] * pow(n0, n1, mod) % mod)
    print(patterns[-1])

Submission status: Wrong Answer

Solution code:
from itertools import groupby
inputs = open(0).readlines()
n = int(inputs[0])
*D, = map(int, inputs[1].split())
D.sort()
mod = 998244353

keys = [k for k, g in groupby(D)]
nodes = [len(list(g)) for k, g in groupby(D)]

if nodes[0] != 1:
    print(0)
else:
    patterns = [1]
    for i, (k, n0, n1) in enumerate(zip(keys, nodes, nodes[1:])):
        if i != k:
            print(0)
            break
        patterns.append(patterns[i] * pow(n0, n1, mod) % mod)
    else:
        if i+1 == keys[-1]:
            print(patterns[-1])
        else:
            print(0)