#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int MAXN = 505;
const int MAXM = 1000005;
int n, m, c[MAXM];
vector<int> v[MAXN];
int a[MAXN];
long long dp[MAXN][MAXN];
long long calc(int L, int R) {
  if (R <= L) return 1;
  if (dp[L][R] != -1) return dp[L][R];
  int j = L;
  long long ret = 0;
  for (int i = L; i <= R; i++) {
    if (a[i] < a[j]) j = i;
    ret = (ret + calc(L, j - 1) * calc(j + 1, i) % MOD * calc(i + 1, R)) % MOD;
  }
  return dp[L][R] = ret;
}
long long solve(int L, int R) {
  if (R < L) return 1;
  int k = 0;
  for (int i = L; i <= R;) {
    a[k++] = c[i];
    i = v[c[i]].back() + 1;
  }
  for (int i = 0; i < k; i++)
    for (int j = 0; j < k; j++) dp[i][j] = -1;
  long long ret = calc(0, k - 1) % MOD;
  for (int i = L; i <= R;) {
    for (int j = 1; j < (int)v[c[i]].size(); j++) {
      ret = ret * solve(v[c[i]][j - 1] + 1, v[c[i]][j] - 1) % MOD;
    }
    i = v[c[i]].back() + 1;
  }
  return ret;
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    scanf("%d", &c[i]);
    c[i]--;
    v[c[i]].push_back(i);
  }
  set<int> s;
  s.insert(-1);
  s.insert(m);
  long long res = 1;
  for (int i = 0; i < n; i++) {
    int L = v[i].front(), R = v[i].back();
    int d = *s.lower_bound(L);
    if (d <= R) {
      printf("0\n");
      return 0;
    }
    for (int x : v[i]) s.insert(x);
  }
  printf("%I64d\n", solve(0, m - 1));
  return 0;
}
