class Fenwick_Tree: def __init__(self, n): self._n = n self.data = [0] * n def add(self, p, x): assert 0 <= p < self._n p += 1 while p <= self._n: self.data[p - 1] += x p += p & -p def sum(self, l, r): assert 0 <= l <= r <= self._n return self._sum(r) - self._sum(l) def _sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s N = int(input()) P = list(map(int, input().split())) mod = 998244353 if N == 1: print(1) exit() D = [0] * N for i in range(N): D[P[i]] = i left = min(D[0], D[1]) right = max(D[0], D[1]) T = Fenwick_Tree(N) for i in range(N): T.add(i, 1) T.add(D[0], -1) T.add(D[1], -1) ans = 1 for i in range(2, N): if D[i] < left: left = D[i] elif D[i] > right: right = D[i] else: ans *= T.sum(left, right + 1) ans %= mod T.add(D[i], -1) print(ans)