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: Accepted

Solution code:
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))


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

cnt = [0 for i in range(N)]

flg = 1
if D[0]!=0:
    flg = 0
for i in range(N):
    if D[i]==0 and i!=0:
        flg = 0
    else:
        cnt[D[i]] += 1

ans = 1
mod = 998244353
flg2 = 1
for i in range(1,N):
    if cnt[i]>0 and flg2:
        ans *= cnt[i-1]**cnt[i]
        ans %= mod
    elif cnt[i]>0 and (not(flg2)):
        flg = 0
    else:
        flg2 = 0
print(ans*flg)

Submission status: Accepted

Solution code:
def powmod(n: int, p: int, M: int) -> int:
    ans = 1
    for _ in range(p):
        ans *= n
        ans %= M
    return ans

def solve():
    N = int(input())

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

    node = [0] * N

    for distance in distances:
        node[distance] += 1

    ans = 1
    MOD = 998244353

    if distances[0] != 0 or node[0] != 1:
        print(0)
    else:
        previous = node[1]
        for i in range(2,N):
            now = node[i]
            ans = ans * powmod(previous, now, MOD) % MOD
            previous = now
        print(ans)
    
solve()

Submission status: Accepted

Solution code:
# import bisect
from collections import Counter, deque
# import copy
# from heapq import heappush, heappop, heapify
# from fractions import gcd
# import itertools
# from operator import attrgetter, itemgetter
# import math

import sys

# import numpy as np

ipti = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n = int(input())
    d = list(map(int,ipti().split()))
    d_max = max(d)
    c = Counter(d)

    if d[0] != 0 or c[0] >= 2:
        print(0)
    else:
        ans = 1
        for i in range(d_max):
            if c[i] == 0:
                ans = 0
                break
            else:
                ans *= c[i] ** c[i+1]
        print(ans%998244353)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

def main():
    N = int(input())
    d = list(map(int,input().split()))
    if d[0] != 0:
        print(0)
    else:
        MOD = 998244353
        use = [0 for _ in range(max(d)+1)]
        for num in d:
            use[num] += 1
        if use[0] != 1:
            print(0)
        else:
            ans,pa = 1,1
            for num in use:
                ans *= pow(pa,num,MOD)
                ans %= MOD
                pa = num
            print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

class Mint:
    @staticmethod
    def get_value(x): return x.value if isinstance(x, Mint) else x

    def __init__(self, val, mod=10**9+7):
        val = Mint.get_value(val)
        self.__mod = mod
        self.__val = val % self.__mod

    @property
    def value(self): return self.__val
    @property
    def inverse(self): return pow(self.__val, self.__mod - 2, self.__mod)

    def __eq__(self, other): return self.__val == other.val
    def __ne__(self, other): return self.__val != other.val

    def __neg__(self): return Mint(self.__val, self.__mod)

    def __iadd__(self, other):
        other = Mint.get_value(other)
        self.__val = (self.__val + other) % self.__mod
        return self
    def __add__(self, other):
        new = Mint(self.__value, self.__mod)
        new += other
        return new
    def __radd__(self, other): return self + other

    def __isub__(self, other):
        other = Mint.get_value(other)
        self.__val = (self.__val - other + self.__mod) % self.__mod
        return self
    def __sub__(self, other):
        new = Mint(self.__value, self.__mod)
        new -= other
        return new
    def __rsub__(self, other):
        new = Mint(Mint.get_value(other), self.__mod)
        new -= self
        return new

    def __imul__(self, other):
        other = Mint.get_value(other)
        self.__val = self.__val * other % self.__mod
        return self
    def __mul__(self, other):
        new = Mint(self.__value, self.__mod)
        new *= other
        return new
    def __rmul__(self, other): return self * other

    def __ifloordiv__(self, other):
        other = Mint(other, self.__mod)
        self *= other.inverse
        return self
    def __floordiv__(self, other):
        new = Mint(self.__value, self.__mod)
        new //= other
        return new
    def __rfloordiv__(self, other):
        new = Mint(Mint.get_value(other), self.__mod)
        new //= self
        return new

C = Counter(D)

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

ans = Mint(1, MOD)
expected = 0
for i,c in sorted(C.items(), key=itemgetter(0)):
    if i == 0: continue
    expected += 1
    if i != expected:
        print(0)
        exit()
    ans *= pow(C[i-1], c, MOD)

print(ans.value)

Submission status: Accepted

Solution code:
mod = 998244353
N = int(input())
D = [int(i) for i in input().split()]
if D[0] != 0:
    print(0)
    exit()
for i in range(1,N):
    if D[i] == 0:
        print(0)
        exit()
def pow_k(a,n,mod): #a**n
    if n == 0:
        return 1
    if n % 2 ==0:
        return pow_k(a*a % mod,n//2,mod)
    else:
        return a * pow_k(a,n-1,mod) % mod
import collections
c = collections.Counter(D)
li = list(c.keys())
li.sort()
n = li[-1]
ans = 1
for i in range(1,n+1):
    if c[i] == 0:
        print(0)
        exit()
    else:
        j = i-1
        k = pow_k(c[j],c[i],mod)
        ans *= k
        ans %= mod
print(ans)

Submission status: Accepted

Solution code:
import collections

mod=998244353
n=int(input())
arr=list(map(int,input().split()))
if arr[0]!=0:
  print(0)
else:
  count=collections.Counter(arr)
  if count[0]!=1:
    print(0)
  else:
    ans=1
    for i in range(1,n):
      ans*=pow(count[i-1],count[i],mod)
      ans%=mod
    print(ans)

Submission status: Accepted

Solution code:
MOD=998244353
class Fp(int):
    def __new__(self,x=0):return super().__new__(self,x%MOD)
    def inv(self):return self.__class__(super().__pow__(MOD-2,MOD))
    def __add__(self,value):return self.__class__(super().__add__(value))
    def __sub__(self,value):return self.__class__(super().__sub__(value))
    def __mul__(self,value):return self.__class__(super().__mul__(value))
    def __floordiv__(self,value):return self.__class__(self*self.__class__(value).inv())
    def __pow__(self,value):return self.__class__(super().__pow__(value%(MOD-1), MOD))
    __radd__=__add__
    __rmul__=__mul__
    def __rsub__(self,value):return self.__class__(-super().__sub__(value))
    def __rfloordiv__(self,value):return self.__class__(self.inv()*value)
    def __iadd__(self,value):self=self+value;return self
    def __isub__(self,value):self=self-value;return self
    def __imul__(self,value):self=self*value;return self
    def __ifloordiv__(self,value):self=self //value;return self
    def __ipow__(self,value):self=self**value;return self
    def __neg__(self):return self.__class__(super().__neg__())

from collections import Counter
N,*D=map(int, open(0).read().split())
ans=Fp(D[0]==0 and 0 not in D[1:])
C=Counter(D)
for k, v in sorted(C.items())[1:]:
    ans*=Fp(C[k-1])**v if k-1 in C else 0
print(ans)

Submission status: Accepted

Solution code:

from collections import Counter
n = int(input())
lst = list(map(int,input().split()))
if lst[0] != 0:
    print(0)
    exit()
mod = 998244353
c = dict(Counter(lst))
c = dict(sorted(c.items(), key=lambda x:x[0]))

for e,(k,v) in enumerate(c.items()):
    if e==0:
        if k!=0 or v!=1:
            ans = 0
            break
        else:
            pre_v = v
            ans = v
    elif e!=k or v==0:
        ans = 0
        break
    else:
        ans = ans*pow(pre_v, v, mod)%mod
        pre_v = v
print(ans%mod)

Submission status: Accepted

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

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

far = 0

cnt = [0 for i in range(N)]

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

    cnt[D[i]] += 1
    far = max(D[i], far)

ans = 1

for i in range(1, far):
    ans *= cnt[i] ** cnt[i + 1]

print(ans % 998244353)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
d = list(map(int, input().split()))
mod = 998244353
cnt = Counter(d)

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

ans = 1
for i in range(len(cnt)+1):
    ans *= pow(cnt[i], cnt[i+1], mod)
    ans %= mod
print(ans)

Submission status: Accepted

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

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

d.sort()
import collections
c = collections.Counter(d)

if c[0] != 1:
    print(0)
    exit()
ans=1
for i in range(1,d[-1]+1):
    if c[i] == 0:
        print(0)
        exit()
    ans*=(c[i-1]**c[i])
    ans%=998244353

print(ans)

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
a = list(map(int,input().split()))
mod = 998244353
if a[0] != 0:
  print(0)
  exit()
ans = 1
c = Counter(a)
cls = list(c.items())
cls.sort()
if cls[0][1] != 1:
  print(0)
  exit()
for i in range(1,len(cls)):
  if cls[i][0] != i:
    print(0)
    exit()
  ans = ans*cls[i-1][1]**cls[i][1]%mod
print(ans)

Submission status: Accepted

Solution code:
mod = 998244353
N = int(input())
D = list(map(int,input().split()))
from collections import Counter
if D[0] != 0 or 0 in D[1:]:
    print(0)
else:
    dc1 = Counter(D[1:])
    dc = [0 for i in range(max(D))]
    for k,v in dc1.items():
        dc[k-1] = v
    ans = 1
    for i in range(1,len(dc)):
        if dc[i] == 0:
            ans = 0
            break
        ans = (ans*(dc[i-1]**dc[i]))%mod
    print(ans)

Submission status: Accepted

Solution code:
from collections import Counter

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

C = Counter(D)

if D[0] > 0 or C[0] > 1:
  print(0)
  exit(0)
  
mod = int(998244353)
ans = 1
for i in range(2,max(D)+1):
  ans = (ans * (C[i-1] ** C[i]) % mod) % mod
  
print(ans)

Submission status: Accepted

Solution code:
def main():
  import sys
  n,*d=map(int,sys.stdin.buffer.read().split())
  e=[0]*n
  
  for i in d:
    e[i]+=1
  
  a=e[0]
  
  for i in d[1:]:
    a=a*e[i-1]%998244353
    
  print(a)
  
if __name__=="__main__":
  main()

Submission status: Accepted

Solution code:
N = int(input())
D = list(map(int, input().split()))
ans = 0
if not D[0] == 0 or not D.count(0) == 1:
    print(0)
else:
    co = [0] * (max(D)+1)
    for i in D:
        co[i] += 1
    ans = 1
    for i in range(2, max(D)+1):
        ans = ans * (co[i-1]**co[i]) % 998244353
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
mod = 998244353
count = [0]*(10**5+1)
L = list(map(int,input().split()))
M = max(L)
ans = 0
for i in range(n):
  count[L[i]] += 1
for i in range(M+1):
  if count[i] == 0:
    ans = 0
    break
  else:
    if i == 0:
      if count[i] != 1:
        ans = 0
        break
      else:
        ans = 1
    else:
      ans = ans*((count[i-1]**count[i])%mod)
      ans = ans%mod
if L[0] == 0:
  print(ans)
else:
  print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import string
import heapq
import _collections
from collections import Counter
from collections import defaultdict
from functools import lru_cache
import bisect
import re
import queue


class Scanner():
    @staticmethod
    def int():
        return int(sys.stdin.readline().rstrip())

    @staticmethod
    def string():
        return sys.stdin.readline().rstrip()

    @staticmethod
    def map_int():
        return [int(x) for x in Scanner.string().split()]

    @staticmethod
    def string_list(n):
        return [input() for i in range(n)]

    @staticmethod
    def int_list_list(n):
        return [Scanner.map_int() for i in range(n)]

    @staticmethod
    def int_cols_list(n):
        return [int(input()) for i in range(n)]


class Math():
    @staticmethod
    def gcd(a, b):
        if b == 0:
            return a
        return Math.gcd(b, a % b)

    @staticmethod
    def lcm(a, b):
        return (a * b) // Math.gcd(a, b)

    @staticmethod
    def roundUp(a, b):
        return -(-a // b)

    @staticmethod
    def toUpperMultiple(a, x):
        return Math.roundUp(a, x) * x

    @staticmethod
    def toLowerMultiple(a, x):
        return (a // x) * x

    @staticmethod
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret

    @staticmethod
    def sign(n):
        if n == 0:
            return 0
        if n < 0:
            return -1
        return 1

    @staticmethod
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3, d + 1, 2):
            if n % i == 0:
                return False
        return True


class PriorityQueue:
    def __init__(self, l=[]):
        self.__q = l
        heapq.heapify(self.__q)
        return

    def push(self, n):
        heapq.heappush(self.__q, n)
        return

    def pop(self):
        return heapq.heappop(self.__q)


MOD = 998244353
INF = int(1e15)


def calc(N):
    return sum(int(x) for x in str(N))


def main():
    # sys.stdin = open("sample.txt")
    N = Scanner.int()
    D = Scanner.map_int()
    if D[0] != 0:
        print(0)
        return
    if D.count(0) != 1:
        print(0)
        return
    C = Counter(D)
    ans = 1
    for i in range(1, len(C) + 1):
        ans *= C[i-1] ** C[i]
        ans %= MOD
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:


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

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

MOD = 998244353

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

key = list(c.keys())
key.sort()

ans = 1
for k in key:
    if k >= 1:
        if k-1 not in c:
            print(0)
            exit()
        else:
            ans *= c[k-1] ** c[k]
            ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
import bisect

n = int(input())
D = list(map(int, input().split()))
if D[0] != 0:
  print(0)
else:
  c = 1
  temp = 1
  e = 1
  D.sort()
  m = D[-1]
  if n > 1 and D[1] == 0:
    print(0)
    exit()
  else:
    for i in range(1, m+1):
      a = bisect.bisect_right(D, i)
      d = a - temp
      temp = a
      if d == 0 and i>0:
        print(0)
        exit()
      else:
        c *= e**d
        c %= 998244353
        e = d
    print(c)

Submission status: Accepted

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

Submission status: Accepted

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

l = [0]*(max(d)+1)
for i in d:
    l[i] += 1

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

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

print(ans%998244353)

Submission status: Accepted

Solution code:
def examA():
    N = I()
    ans = (N-1)//2
    print(ans)
    return

def examB():
    N = I()
    D = LI()
    d = Counter(D)
    loop = max(d.keys())
    if D[0]!=0 or d[0]!=1:
        print(0)
        return
#    print(d,loop)
    ans = 1; cur = 1
    for i in range(loop+1):
        if d[i]==0:
            print(0)
            return
        for _ in range(d[i]):
            ans *= cur
            ans %= mod2
        cur = d[i]
    print(ans)
    return

def examC():
    ans = 0
    print(ans)
    return

def examD():
    ans = 0
    print(ans)
    return

def examE():
    ans = 0
    print(ans)
    return

def examF():
    ans = 0
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LFI(): return list(map(float,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
global mod,mod2,inf,alphabet
mod = 10**9 + 7
mod2 = 998244353
inf = 10**18
alphabet = [chr(ord('a') + i) for i in range(26)]

if __name__ == '__main__':
    examB()

"""

"""

Submission status: Accepted

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

nodes = [0] * N

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

ans = 0
if nodes[0]==1 and D[0]==0:
  pre_n = 1
  ans = 1
  for n in nodes:
    ans = (ans * pre_n**n) % MOD
    pre_n = n

print(ans)

Submission status: Accepted

Solution code:
def main():
  import sys
  n,*d=map(int,sys.stdin.buffer.read().split())
  e=[0]*n
  for i in d:e[i]+=1
  a=1if d[0]<1else 0
  for i in d[1:]:a=a*e[i-1]%998244353
  print(a)
main()

Submission status: Accepted

Solution code:
def main():
  n,*d=map(int,open(0).read().split())
  e=[0]*n
  for i in d:e[i]+=1
  a=1if d[0]<1else 0
  for i in d[1:]:a=a*e[i-1]%998244353
  print(a)
main()

Submission status: Accepted

Solution code:
def main():
  from collections import Counter
  M=998244353
  n,*d=map(int,open(0).read().split())
  e=Counter(d)
  a=1if d[0]<1==e[0]else 0
  for i in range(n):a*=pow(e[i],e[i+1],M)
  print(a%M)
main()

Submission status: Accepted

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

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

ans = 1
for d in D[1:]:
    ans = ans * counter[d-1] % mod
print(ans)

Submission status: Accepted

Solution code:
import sys
read = lambda: sys.stdin.readline().rstrip()

class Solution(object):
    def counting_tree(self, N, D):
        #import collections
        tot = 1

        #cnt = collections.Counter(D)

        cnt = [0]*N
        for i in D:
            cnt[i]+=1

        for i in D[1:]:
            tot = tot * cnt[i-1]%998244353

        return tot if D[0]==0 else 0

def main():
    
    N0 = int(read())
    D0 = tuple(map(int, read().split()))

    res = Solution().counting_tree(N0, D0)

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict


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


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

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

cnt = defaultdict(int)
for d in D:
    cnt[d] += 1

ans = 1
for i in range(1, max(D) + 1):
    if cnt.get(i) is None:
        print(0)
        exit(0)
    num = pow(cnt[i - 1], cnt[i], MOD)
    ans = ans * num % MOD
print(ans)

Submission status: Accepted

Solution code:
import sys
read = lambda: sys.stdin.readline().rstrip()

def counting_tree(N, D):
    import collections
    tot = 1

    cnt = collections.Counter(D)
    """
    cnt = [0]*N
    for i in D:
        cnt[i]+=1
    """

    for i in D[1:]:
        tot = tot * cnt[i-1]%998244353

    return tot if D[0]==0 else 0

def main():
    
    N0 = int(read())
    D0 = tuple(map(int, read().split()))

    res = counting_tree(N0, D0)

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
read = lambda: sys.stdin.readline().rstrip()

def counting_tree(N, D):
    tot = 1
    
    # cnt = collections.Counter(D)
    cnt = [0]*N
    for i in D:
        cnt[i]+=1

    for i in D[1:]:
        tot = tot * cnt[i-1]%998244353

    return tot if D[0]==0 else 0

def main():
    
    N0 = int(read())
    D0 = tuple(map(int, read().split()))

    res = counting_tree(N0, D0)

    print(res)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

li = [int (i) for i in input().split()]

cnt = [0] * n

for i in li:
    cnt[i] += 1

ans = 1

for i in li[1:]:
    ans = ans*cnt[i-1]%998244353
if li[0] != 0:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
a = int(input())
l = list(map(int,input().split()))
d = [0]*a
for i in l:
    d[i]+=1
r =1
if  l[0]!=0:
    print(0)
    exit()
for i in l[1:]:
    r=r*d[i-1]%998244353
print(r)

Submission status: Accepted

Solution code:
a = int(input())
l = list(map(int,input().split()))
MOD = 998244353
dp = [0]*a
for i in l:
    dp[i]+=1
res = 1
# print(dp)
if  l[0]!=0:
    print(0)
    exit()

for i in l[1:]:
    # print(i)
    res*=dp[i-1]
    res%=MOD

print(res%MOD)

Submission status: Accepted

Solution code:
a = int(input())
l = list(map(int,input().split()))
MOD = 998244353
dp = [0]*a
for i in l:
    dp[i]+=1
res = 1
# print(dp)
if dp[0] != 1 and l[0]!=0:
    print(0)
    exit()

for i in l[1:]:
    # print(i)
    res*=dp[i-1]
    res%=MOD

print(res)

Submission status: Accepted

Solution code:
n = int(input())
d = [int(i) for i in input().split()]
m = max(d)
cnt = [0]*(m+1)
ans = 1
flag = True
mod = 998244353

if d[0] == 0:
    cnt[0] += 1
else:
    flag = False

for i in range(1,n):
    if d[i] == 0:
        flag == False
        break
    else:
        cnt[d[i]] += 1

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

if flag == False:
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate
import sys
import bisect
import string
import math
import time


def I(): return int(input())


def S(): return input()


def MI(): return map(int, input().split())


def MS(): return map(str, input().split())


def LI(): return [int(i) for i in input().split()]


def LI_(): return [int(i)-1 for i in input().split()]


def StoI(): return [ord(i)-97 for i in input()]


def ItoS(nn): return chr(nn+97)


def input(): return sys.stdin.readline().rstrip()


def show(*inp, end='\n'):
    if show_flg:
        print(*inp, end=end)


YN = ['No', 'Yes']
MOD = 10**9+7
inf = float('inf')
IINF = 10**10
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

show_flg = False
# show_flg = True


def main():
    MOD = 998244353
    N = I()
    D = LI()

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

    if any([d == 0 for d in D[1:]]):
        print(0)
        return

    MAX_D = max(D)
    node = [0] * (MAX_D + 1)

    for i in range(N):
        node[D[i]] += 1

    # print(node)
    ans = 1
    for i in range(1, len(node)):
        ans *= node[i-1] ** node[i]
        ans %= MOD
    print(ans % MOD)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import collections

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

if D[0]==0 and data[0]==1:
    count=1
    for i in range(1,len(data)):
        count*=data[i-1]**data[i]
        count%=mod
    print(count)
else:
    print(0)

Submission status: Accepted

Solution code:
n = int(input())
lis = list(map(int,input().split()))
num = [0 for i in range(max(lis)+1)]
if lis[0] != 0:
    print(0)
    exit()
if lis.count(0) > 1:
    print(0)
    exit()
for nu in lis:
    num[nu] += 1
ans = 1
for i in range(max(lis)):
    if num[i] == 0:
        print(0)
        exit()
    for _ in range(num[i+1]):
        ans *= num[i]
        ans %= 998244353
print(ans%998244353)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
mod2 = 998244353
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = LI()
    if a[0] != 0:
        return 0
    c = collections.Counter(a)
    r = 1
    for i in range(1,n):
        r *= c[a[i]-1]
        r %= mod2

    return r


print(main())

Submission status: Accepted

Solution code:
N=int(input())
D=list(map(int,input().split()))
layer=[0]*N

mx=0
for item in D:
    layer[item]+=1
    mx=max(mx,item)

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

ans=1
for i in range(mx):
    ans*=(layer[i]**layer[i+1])

print(ans%998244353)

Submission status: Accepted

Solution code:
def mod_pow(x, n, MOD):
    if n == 0:
        return 1
    if n % 2 == 0:
        result = mod_pow(x * x, n // 2, MOD)
        return result % MOD
    else:
        result = x * mod_pow(x, n - 1, MOD)
        return result % MOD

from collections import Counter        

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

count = Counter(D)
ans = 1

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

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

for i in range(max_num):

    ans *= mod_pow(count[i], count[i+1], MOD)
    ans %= MOD

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
d=list(map(int,input().split()))
dic=dict()
for i in range(n):
  if d[i] not in dic:
    dic[d[i]]=1
  else:
    dic[d[i]]+=1
if d[0]!=0 or dic[0]>1:
  print(0)
else:
  l=list(dic.items())
  l.sort()
  ans=1
  for i in range(len(l)-1):
    if l[i][0]+1==l[i+1][0]:
      ans*=l[i][1]**l[i+1][1]
    else:
      print(0)
      exit()
  print(ans%998244353)

Submission status: Accepted

Solution code:
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
import scipy
from collections import deque
#https://atcoder.jp/contests/nikkei2019-2-qual/tasks/nikkei2019_2_qual_b?lang=en
#B - Counting of Trees
"""
sort, and then check how many next levels we have against the previes level
keep a prevcount, and each level is prev level ** cur elvel
"""
N = gi()
cnt = [0] * N
dist = [0] * N

MOD = 998244353

max_d = 0

for i in range(N):
    dist[i] = gi()
    cnt[dist[i]] += 1
    max_d = max(max_d, dist[i])


def run():
    if cnt[0] != 1 or dist[0] > 0:
        print(0)
        return

    ans = 1
    for i in range(1, max_d + 1):
        if cnt[i] == 0:
            print(0)
            return
        for _ in range(cnt[i]):
            ans *= cnt[i - 1]
            ans %= MOD

    print(ans)


run()

Submission status: Accepted

Solution code:
from collections import Counter
MOD = 998244353
def solve(n, d):
    if d[0] > 0:
        return 0
    d = sorted(d)  
    if (n > 1) and (d[1] == 0):
        return 0
    D = Counter(d) 
    k = sorted(list(D.keys()))
    m = len(k)  
    if m -1 != k[-1]:
        return 0
    res = 1
    for i in range(1, m):
        p = D[k[i-1]]
        q = D[k[i]]
        res = (res * (p**q)) % MOD
    return res
n = int(input())
d = list(map(int, input().split()))
print(solve(n, d))

Submission status: Accepted

Solution code:
n = int(input())
mod = 998244353
data = [0]*(n+1)
d = list(map(int,input().split()))
if d[0] != 0:
    print(0)
    exit()
for x in d:
    data[x] += 1

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

q = 0
ans = 1
for i in range(1,n):
    ans *= pow(data[i-1],data[i],mod)
    ans %= mod

print(ans)

Submission status: Accepted

Solution code:
import collections
 
N = int(input())
D = list(map(int, input().split()))
 
c = collections.Counter(D)
 
if c[0] != 1:
    print(0)
elif D[0] != 0:
    print(0)
else:
    ans = 1
    buf = 1
    for i in range(max(D) + 1):
        if c[i] == 0:
            print(0)
            break
        ans *= buf ** c[i]
        buf = c[i]
    else:
        print(ans % 998244353)

Submission status: Accepted

Solution code:
from collections import Counter

def print_0():
  print(0)
  exit()

MOD = 998244353
N = int(input())
inputD = list(map(int,input().split()))
D = Counter(inputD)
if inputD[0] != 0 or D[0]!=1: print_0()

max_rank = max(D.keys())
ans = 1
for i in range(max_rank+1):
  if i not in D:
    print_0()
  elif i not in [0,1]:
    ans *= D[i-1]**D[i]
print(ans%MOD)

Submission status: Accepted

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

zero_flag = False
if dic_c[0]!= 1 or dd[0]!=0 :
  zero_flag = True

for i in range(max(dd)):
  if dic_c[i] == 0 :
    zero_flag = True

if zero_flag:
  print(0)  
else:
  ans = 1
  for i in range(1,max(dd)):
    ans *= dic_c[i]**dic_c[i+1]
  print(ans % 998244353)

Submission status: Accepted

Solution code:
from collections import Counter

n = int(input())
d = [int(i) for i in input().split()]
c = Counter(d)

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

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

print(ans % 998244353)

Submission status: Accepted

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