#include <bits/stdc++.h>
using namespace std;
const int M = 1e6 + 9, N = 1003, mod = 998244353;
int n, m, x, p, mn, last, c[M], t[N], cnt[N], dp[N][N];
int calc(int l, int r) {
  int mn_el = N, mn_an = N, mx_an = 0, last_an = -1;
  int res = 1, ans1 = 0, ans2 = 0;
  if (l >= r) return 1;
  if (dp[l][r] != -1) return dp[l][r];
  for (int i = l; i < r + 1; i++) {
    mn_el = min(mn_el, c[i]);
    ;
    cnt[c[i]] = 0;
  }
  for (int i = l; i < r + 1; i++) cnt[c[i]]++;
  for (int i = l; i < r + 1; i++)
    if (cnt[c[i]] != t[c[i]]) return dp[l][r] = 0;
  for (int i = l; i < r + 1; i++)
    if (c[i] == mn_el) {
      if (last_an != -1) res = 1ll * res * calc(last_an + 1, i - 1) % mod;
      last_an = i;
      mx_an = max(mx_an, i);
      ;
      mn_an = min(mn_an, i);
      ;
    }
  for (int i = l - 1; i < mn_an; i++)
    ans1 = (ans1 + 1ll * calc(l, i) * calc(i + 1, mn_an - 1) % mod) % mod;
  for (int i = mx_an; i < r + 1; i++)
    ans2 = (ans2 + 1ll * calc(mx_an + 1, i) * calc(i + 1, r) % mod) % mod;
  dp[l][r] = 1ll * res * ans1 % mod * ans2 % mod;
  return dp[l][r];
}
int main() {
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) dp[i][j] = -1;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    scanf("%d", &x);
    ;
    if (x != last) c[p++] = x;
    last = x;
  }
  m = p;
  if (n * 2 <= m) return cout << 0, 0;
  for (int i = 0; i < m; i++) {
    t[c[i]]++;
    mn = N;
    for (int j = i + 2; j < m; j++) {
      mn = min(mn, c[j - 1]);
      ;
      if (c[i] == c[j] && mn < c[i]) return cout << 0, 0;
    }
  }
  cout << calc(0, m - 1);
}
