#include <bits/stdc++.h>
using namespace std;
const double EPS = 1e-9;
const long long MOD = 998244353;
const int inf = 1 << 30;
const long long linf = 1LL << 60;
template <typename T>
struct segtree {
  int n;
  T dat[2 * (1 << 20) - 1];
  void init(int n_) {
    n = 1;
    while (n < n_) n *= 2;
    for (int i = 0; i < 2 * n - 1; i++) dat[i] = inf;
  }
  void update(int k, T a) {
    k += n - 1;
    dat[k] = a;
    while (k > 0) {
      k = (k - 1) / 2;
      dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
  }
  T query(int a, int b, int k, int l, int r) {
    if (r <= a || b <= l) return inf;
    if (a <= l && r <= b)
      return dat[k];
    else {
      T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
      T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
      return min(vl, vr);
    }
  }
};
segtree<int> seg;
long long n, m;
int c[1000001];
unordered_map<long long, int> dp;
vector<int> pos[501], hoge[501];
int lj[1000001], rj[1000001];
bool saw[501];
bool checker() {
  stack<int> sta;
  for (int i = 0; i < m; i++) {
    while (!sta.empty() && sta.top() > c[i]) {
      sta.pop();
    }
    if (saw[c[i]] && sta.top() != c[i]) {
      return false;
    }
    sta.push(c[i]);
    saw[c[i]] = true;
  }
  return true;
}
long long solve(int l, int r) {
  if (r - l <= 1) return 1;
  if (dp.count(l * MOD + r)) return dp[l * MOD + r];
  long long ret = 1;
  int mid = seg.query(l, r, 0, 0, seg.n);
  long long tmp = 0;
  for (int i = l; i <= pos[mid][0]; i = lj[i]) {
    tmp = (tmp + solve(l, i) * solve(i, pos[mid][0])) % MOD;
  }
  ret = ret * tmp % MOD;
  tmp = 0;
  for (int i = pos[mid][pos[mid].size() - 1] + 1; i <= r; i = lj[i]) {
    tmp =
        (tmp + solve(pos[mid][pos[mid].size() - 1] + 1, i) * solve(i, r)) % MOD;
  }
  ret = ret * tmp % MOD;
  for (int i = 0; i < hoge[mid].size(); i++) {
    ret = ret * solve(pos[mid][hoge[mid][i] - 1] + 1, pos[mid][hoge[mid][i]]) %
          MOD;
  }
  return dp[l * MOD + r] = ret;
}
int main() {
  scanf("%lld%lld", &n, &m);
  seg.init(m);
  for (int i = 0; i < m; i++) scanf("%d", c + i);
  for (int i = 0; i < m; i++) c[i]--;
  for (int i = 0; i < m; i++) seg.update(i, c[i]);
  for (int i = 0; i < m; i++) pos[c[i]].push_back(i);
  for (int i = 0; i < n; i++) {
    for (int j = 1; j < pos[i].size(); j++) {
      if (pos[i][j] - pos[i][j - 1] > 1) {
        hoge[i].push_back(j);
      }
    }
  }
  for (int i = 0; i < m + 1; i++) lj[i] = i + 1;
  for (int i = 0; i < n; i++) {
    lj[pos[i][0]] = pos[i][pos[i].size() - 1] + 1;
  }
  if (!checker()) {
    puts("0");
    return 0;
  }
  printf("%lld\n", solve(0, m));
}
