#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 3, M = 1e6 + 3, Mod = 998244353;
struct Mint {
  int res;
  Mint() { res = -1; }
  Mint(int a) : res(a) {}
  friend Mint operator+(Mint A, Mint B) {
    return (A.res + B.res >= Mod) ? (A.res + B.res - Mod) : (A.res + B.res);
  }
  friend Mint operator-(Mint A, Mint B) {
    B.res = Mod - B.res;
    return A + B;
  }
  friend Mint operator*(Mint A, Mint B) { return 1ll * A.res * B.res % Mod; }
};
int n, m, c[M], L[N], R[N], Mn[N][N];
Mint dp[N][N];
Mint F(int l, int r) {
  if (r <= l) return 1;
  if (~dp[l][r].res) return dp[l][r];
  if (L[Mn[l][r]] < l || R[Mn[l][r]] > r) return dp[l][r] = Mint(0);
  if (r - l <= 1) return dp[l][r] = Mint(1);
  Mint Lprd = Mint(0), Rprd = Mint(0), Mprd = Mint(1);
  for (register int i = (l); i <= (L[Mn[l][r]]); ++i)
    Lprd = Lprd + F(i, L[Mn[l][r]]) * F(l, i);
  for (register int i = (R[Mn[l][r]]); i <= (r); ++i)
    Rprd = Rprd + F(R[Mn[l][r]], i) * F(i, r);
  int lst = L[Mn[l][r]];
  for (register int i = (L[Mn[l][r]] + 1); i <= (R[Mn[l][r]]); ++i)
    if (c[i] == Mn[l][r]) Mprd = Mprd * F(lst + 1, i), lst = i;
  return dp[l][r] = Lprd * Rprd * Mprd;
}
int main() {
  scanf("%d%d", &n, &m);
  for (register int i = (1); i <= (m); ++i) scanf("%d", c + i);
  m = unique(c + 1, c + m + 1) - c - 1;
  if (m > 2 * n) return puts("0"), 0;
  for (register int i = (1); i <= (n); ++i) L[i] = m + 1, R[i] = 0;
  for (register int i = (1); i <= (m); ++i)
    L[c[i]] = min(L[c[i]], i), R[c[i]] = max(R[c[i]], i + 1),
    Mn[i][i + 1] = c[i];
  for (register int l = (1); l <= (m); ++l)
    for (register int r = (l + 2); r <= (m + 1); ++r)
      Mn[l][r] = min(Mn[l][r - 1], c[r - 1]);
  printf("%d\n", F(1, m + 1).res);
  return 0;
}
