#include <bits/stdc++.h>
const int N = 1e3 + 5, M = 1e6 + 5;
const int MOD = 998244353;
int n, m, a[M], f[N][N];
std::vector<int> vec[N];
void add(int &x, int y) { (x += y) >= MOD && (x -= MOD); }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) scanf("%d", &a[i]);
  m = std::unique(a + 1, a + m + 1) - (a + 1);
  if (m > 2 * n) {
    printf("0\n");
    return 0;
  }
  for (int i = 1; i <= m; i++) vec[a[i]].emplace_back(i);
  for (int i = 1; i <= m + 1; i++) f[i][i - 1] = 1;
  for (int l = 1; l <= m; l++) {
    for (int i = 1, j = l; j <= m; i++, j++) {
      int c = *std::min_element(a + i, a + j + 1);
      int x = vec[c].front(), y = vec[c].back();
      if (x < i || y > j) {
        f[i][j] = 0;
        continue;
      }
      int L = 0, R = 0;
      for (int k = i; k <= x; k++) {
        add(L, 1LL * f[i][k - 1] * f[k][x - 1] % MOD);
      }
      for (int k = y; k <= j; k++) {
        add(R, 1LL * f[y + 1][k] * f[k + 1][j] % MOD);
      }
      f[i][j] = 1LL * L * R % MOD;
      for (int k = 1; k < (int)vec[c].size(); k++) {
        if (vec[c][k - 1] + 1 < vec[c][k]) {
          f[i][j] = 1LL * f[i][j] * f[vec[c][k - 1] + 1][vec[c][k] - 1] % MOD;
        }
      }
    }
  }
  printf("%d\n", f[1][m]);
  return 0;
}
