#include <bits/stdc++.h>
using namespace std;
const int MAXM = 1e6 + 5, MAXN = 1005, Mod = 998244353;
char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc;
int ny;
inline char gc() {
  return pp1 == pp2 &&
                 (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2)
             ? EOF
             : *pp1++;
}
inline int read() {
  int x = 0;
  for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;)
    if (nc == 45) ny = -1;
  if (nc < 0) return nc;
  for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF;
       x = (x << 3) + (x << 1) + (nc ^ 48))
    ;
  return x * ny;
}
int n, m, a[MAXM], L[MAXN], R[MAXN], f[MAXN][MAXN];
int main() {
  n = read();
  for (int t = read(), x; t--;) x = read(), x != a[m] ? a[++m] = x : 0;
  if (m > 2 * n) return puts("0"), 0;
  for (int i = 1; i <= n; i++) L[i] = m + 1;
  for (int i = 1; i <= m; i++) L[a[i]] = min(L[a[i]], i), R[a[i]] = i;
  for (int i = 1; i <= m; i++)
    if (L[a[i]] == R[a[i]]) f[i][i] = 1;
  for (int i = 0; i <= m; i++) f[i + 1][i] = 1;
  for (int len = 2; len <= m; len++)
    for (int l = 1; l <= m - len + 1; l++) {
      int r = l + len - 1;
      int Mn = a[l], p = l;
      for (int i = l; i <= r; i++)
        if (a[i] < Mn) Mn = a[i], p = i;
      int x = L[Mn], y = R[Mn];
      if (x < l || y > r) continue;
      int sumL = 0, sumR = 0;
      for (int i = l; i <= x; i++)
        sumL = (sumL + 1ll * f[l][i - 1] * f[i][x - 1]) % Mod;
      for (int i = y; i <= r; i++)
        sumR = (sumR + 1ll * f[y + 1][i] * f[i + 1][r]) % Mod;
      f[l][r] = 1ll * sumL * sumR % Mod;
      int last = x;
      for (int i = x + 1; i <= y; i++)
        if (a[i] == Mn)
          f[l][r] = 1ll * f[l][r] * f[last + 1][i - 1] % Mod, last = i;
    }
  cout << f[1][m] << "\n";
  return 0;
}
