import numpy as np
import sys

M = 998244353
N = int(input())
A = np.empty(N, dtype=np.int64)
for i in range(N):
    A[i] = int(input())

if A[0]==A[N-1]==1:
    nps = np.where(A!=1)[0]
    if nps.max() - nps.min() + 1 != len(nps):
        print(0)
        sys.exit()
    else:
        k = N - len(nps)
else:
    ps = np.where(A==1)[0]
    if ps.max() - ps.min() + 1 != len(ps):
        print(0)
        sys.exit()
    else:
        k = len(ps)

if k == 0:
    print(0)
    sys.exit()

result = 1
killed = 0
for i in range(2,N+1):
    if A[0]==A[N-1]==i:
        nps = np.where(A!=i)[0]
        a = nps.min()
        b = nps.max()
        if b - a + 1 != len(nps):
            print(0)
            sys.exit()
        else:
            length = N - len(nps)
            head = A[a]
            tail = A[b]
    else:
        ps = np.where(A==i)[0]
        length = len(ps)
        if length > 0:
            a = ps.min()
            b = ps.max()
            if b - a + 1 != len(ps):
                print(0)
                sys.exit()
            else:
                if a == 0:
                    tail = A[N-1]
                else:
                    tail = A[a-1]
                if b == N-1:
                    head = A[0]
                else:
                    head = A[b+1]

    if length == 0:
        result = (result * killed) % M
        killed = killed - 1
    elif length > k:
        print(0)
        exit()
    elif head > i and tail > i:
        if length != k:
            print(0)
            exit()
    elif head < i and tail < i:
        result = (result * (k-length+1)) % M
        killed = killed + (length+k-2)
    else:
        # head < i and tail > i
        # head > i and tail < i
        killed = killed + (length-1)

print(result)