#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma warning(disable : 4996)
const double PI = acos(-1);
const double eps = 1e-6;
const long long inf = 1e9;
inline long long lgput(long long a, long long b, long long mod) {
  long long ret = 1;
  while (b) {
    if (b & 1) ret = (ret * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return (ret % mod);
}
inline long long inv(long long x, long long MOD) {
  return lgput(x, MOD - 2, MOD);
}
inline bool exist(double nr) { return (nr < -eps) || (nr > eps); }
long long big_rand() { return rand() * RAND_MAX + rand(); }
const int MAXM = 1100;
const int MAXN = 505;
const int MOD = 998244353;
int v[1000100];
vector<int> poz[MAXN];
int MIN[MAXM][MAXM];
long long dp[MAXM][MAXM];
int interst[MAXN][MAXM];
int interdr[MAXN][MAXM];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  cout << setprecision(10) << fixed;
  mt19937 rng(time(nullptr));
  srand(time(nullptr));
  int n, m;
  cin >> n >> m;
  int pnt = 0;
  for (int i = 1; i <= m; i++) {
    int nr;
    cin >> nr;
    if (nr == v[pnt]) {
      continue;
    }
    pnt++;
    v[pnt] = nr;
    poz[v[pnt]].push_back(pnt);
  }
  m = pnt;
  if (m > 2000) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = poz[i][0] + 1; j <= poz[i][(int)poz[i].size() - 1] - 1; j++) {
      if (v[j] < i) {
        cout << 0;
        return 0;
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    MIN[i][i] = v[i];
    for (int j = i + 1; j <= m; j++) {
      MIN[i][j] = min(MIN[i][j - 1], v[j]);
    }
  }
  for (int i = 0; i <= m + 1; i++) {
    for (int j = 0; j <= m + 1; j++) {
      dp[i][j] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = poz[i][0] + 1; j <= poz[i][(int)poz[i].size() - 1]; j++) {
      interst[i - 1][j] = 1;
    }
    for (int j = poz[i][0]; j <= poz[i][(int)poz[i].size() - 1] - 1; j++) {
      interdr[i - 1][j] = 1;
    }
  }
  for (int i = n; i >= 1; i--) {
    for (int j = 1; j <= m; j++) {
      interst[i][j] = max(interst[i][j], interst[i + 1][j]);
      interdr[i][j] = max(interdr[i][j], interdr[i + 1][j]);
    }
  }
  for (int l = 1; l < m; l++) {
    for (int i = 1; i + l <= m; i++) {
      int j = i + l;
      int now = MIN[i][j];
      if (poz[now][0] < i || poz[now][(int)poz[now].size() - 1] > j) {
        continue;
      }
      long long st = 0, dr = 0;
      for (int k = i; k <= poz[now][0]; k++) {
        if (interst[now][k]) {
          continue;
        }
        st += dp[i][k - 1] * dp[k][poz[now][0] - 1];
        st %= MOD;
      }
      for (int k = poz[now][(int)poz[now].size() - 1]; k <= j; k++) {
        if (interdr[now][k]) {
          continue;
        }
        dr += dp[poz[now][(int)poz[now].size() - 1] + 1][k] * dp[k + 1][j];
        dr %= MOD;
      }
      st = max(st, 1LL);
      dr = max(dr, 1LL);
      dp[i][j] = st * dr;
      dp[i][j] %= MOD;
      for (int k = 0; k < (int)poz[now].size() - 1; k++) {
        dp[i][j] *= dp[poz[now][k] + 1][poz[now][k + 1] - 1];
        dp[i][j] %= MOD;
      }
    }
  }
  cout << dp[1][m];
}
