#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1010, M = 1000006;
const ll mod = 998244353;
int n, m, c[M];
ll dp[N][N];
int cnt[N];
ll f(int L, int R) {
  if (L > R) return 1;
  ll &ans = dp[L][R];
  if (~ans) return ans;
  int id = L, id2 = L;
  for (int i = L + 1; i <= R; i++) {
    if (c[i] < c[id]) id = i;
    if (c[i] <= c[id2]) id2 = i;
  }
  int in_cnt = 0;
  for (int i = L; i <= R; i++)
    if (c[i] == c[id]) in_cnt++;
  if (in_cnt != cnt[c[id]]) return ans = 0;
  if (L == R) return ans = 1;
  ll a = 0, b = 0;
  for (int i = L; i <= id; i++) {
    a = (a + f(L, i - 1) * f(i, id - 1)) % mod;
  }
  for (int j = id2; j <= R; j++) {
    b = (b + f(id2 + 1, j) * f(j + 1, R)) % mod;
  }
  ll x = 1;
  for (int j = id; j < id2; j++) {
    int tmp = j + 1;
    while (tmp <= id2 and c[tmp] != c[id]) tmp++;
    x = x * f(j + 1, tmp - 1) % mod;
    j = tmp - 1;
  }
  return ans = a * b % mod * x % mod;
}
int main() {
  memset(dp, -1, sizeof dp);
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d", c + i);
  {
    int j = 0;
    for (int i = 1; i < m; i++) {
      if (c[i] != c[j]) {
        c[++j] = c[i];
      }
    }
    m = j + 1;
  }
  if (m > 2 * n + 5) return printf("0\n"), 0;
  for (int i = 0; i < m; i++) cnt[c[i]]++;
  printf("%lld\n", f(0, m - 1));
}
