#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
const int maxn = 555;
const int maxm = 1000010;
int N, M;
int C[maxm], D[maxn], del[maxm], go[maxm];
pair<int, int> mn[maxn << 1][maxn << 1];
vector<int> V[maxn];
set<pair<int, int> > st;
set<int> vis;
int cc[maxn][maxn];
int dp(int l, int r) {
  if (l > r) return 1;
  int &ret = cc[l][r];
  if (ret != -1) return ret;
  if (l == r) return ret = 1;
  int t = mn[l][r].second;
  ret = 0;
  if (t == l) {
    for (int i = l; i <= r; i++) {
      ret += 1LL * dp(l + 1, i) * dp(i + 1, r) % mod;
      ret %= mod;
    }
    return ret;
  }
  if (t == r) {
    for (int i = r; i >= l; i--) {
      ret += 1LL * dp(i, r - 1) * dp(l, i - 1) % mod;
      ret %= mod;
    }
    return ret;
  }
  return ret = 1LL * dp(l, t) * dp(t, r) % mod;
}
int solve(int n) {
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      mn[i][j] = pair<int, int>(D[j], j);
      if (i != j) mn[i][j] = min(mn[i][j], mn[i][j - 1]);
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) cc[i][j] = -1;
  return dp(0, n - 1);
}
int main() {
  scanf("%d %d", &N, &M);
  for (int i = 0; i < M; i++) {
    scanf("%d", &C[i]);
    V[C[i]].push_back(i);
  }
  memset(go, -1, sizeof(go));
  st.insert(pair<int, int>(0, M - 1));
  for (int i = 1; i <= N; i++) {
    auto it = st.lower_bound(pair<int, int>(V[i][0], 1e9));
    pair<int, int> t = *(--it);
    st.erase(t);
    if (t.second < V[i].back()) {
      printf("0");
      return 0;
    }
    st.insert(pair<int, int>(t.first, V[i][0]));
    st.insert(pair<int, int>(V[i].back(), t.second));
    for (int j = 0; j < (int)V[i].size() - 1; j++) {
      if (V[i][j] + 1 < V[i][j + 1])
        st.insert(pair<int, int>(V[i][j] + 1, V[i][j + 1] - 1));
    }
    for (int j = 1; j < (int)V[i].size() - 1; j++) {
      del[V[i][j]] = 1;
    }
    if (V[i][0] != V[i].back()) go[V[i][0]] = V[i].back();
  }
  for (int i = 0; i < M; i++)
    if (!del[i]) vis.insert(i);
  int ans = 1;
  while (vis.size()) {
    int u = *vis.begin();
    int n = 0;
    while (1) {
      if (vis.find(u) == vis.end()) break;
      D[n++] = C[u];
      vis.erase(u);
      if (go[u] != -1) {
        u = go[u];
        vis.erase(u);
      }
      u++;
    }
    ans = 1LL * ans * solve(n) % mod;
  }
  printf("%d", ans);
}
