N=int(eval(input()))

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



mod=998244353



if D[0]!=0:

    print((0))

    exit()



n=[0]*N

n[0]=1

for i in range(1,N):

    if D[i]==0:

        print((0))

        exit()

    n[D[i]]+=1



ans=1

cnt=n[0]+n[1]

for i in range(2,N):

    if n[i]==0:

        if cnt!=N:

            print((0))

            exit()

        else:

            break

    ans=ans*n[i-1]**n[i]

    ans=ans%mod

    cnt=cnt+n[i]

print(ans)

N=int(eval(input()))

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

mod=998244353



if D[0]!=0:

    print((0))

    exit()





B=[0]*N

for d in D:

    B[d]+=1

if B[0]!=1:

    print((0))

    exit()



ans=1

total=1

for i in range(1,N):

    if B[i]==0:

        if total!=N:

            print((0))

            exit()

        else:break

    total+=B[i]

    ans*=(B[i-1]**B[i])

    ans%=mod



print(ans)
N = int(eval(input()))
D = list(map(int, input().split()))
mod = 998244353
if D[0] != 0:
    print((0))
    exit()
n = [0] * N
n[0] = 1
for i in range(1, N):
    if D[i] == 0:
        print((0))
        exit()
    n[D[i]] += 1
ans = 1
cnt = n[0] + n[1]
for i in range(2, N):
    if n[i] == 0:
        if cnt != N:
            print((0))
            exit()
        else:
            break
    ans = ans * n[i - 1] ** n[i]
    ans = ans % mod
    cnt = cnt + n[i]
print(ans)

N = int(eval(input()))
D = list(map(int, input().split()))
mod = 998244353
if D[0] != 0:
    print((0))
    exit()
B = [0] * N
for d in D:
    B[d] += 1
if B[0] != 1:
    print((0))
    exit()
ans = 1
total = 1
for i in range(1, N):
    if B[i] == 0:
        if total != N:
            print((0))
            exit()
        else:
            break
    total += B[i]
    ans *= B[i - 1] ** B[i]
    ans %= mod
print(ans)

-n = [0] * N -n[0] = 1 +B = [0] * N +for d in D: +    B[d] += 1 +if B[0] != 1: +    print((0)) +    exit() +ans = 1 +total = 1 -    if D[i] == 0: -        print((0)) -        exit() -    n[D[i]] += 1 -ans = 1 -cnt = n[0] + n[1] -for i in range(2, N): -    if n[i] == 0: -        if cnt != N: +    if B[i] == 0: +        if total != N: -    ans = ans * n[i - 1] ** n[i] -    ans = ans % mod -    cnt = cnt + n[i] +    total += B[i] +    ans *= B[i - 1] ** B[i] +    ans %= mod