#include <bits/stdc++.h>
using namespace std;
int n, m, tot, a[1000005];
long long dp[1005][1005];
vector<int> p[1005];
int rd() {
  int x = 0, tp = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') tp = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * tp;
}
long long getans(int l, int r) {
  if (l > r) return 1;
  if (dp[l][r] != -1) return dp[l][r];
  int mn = a[l], L, R, sz;
  for (int i = l + 1; i <= r; i++) mn = min(a[i], mn);
  long long nl = 0, nr = 0;
  sz = p[mn].size(), L = p[mn][0], R = p[mn][sz - 1];
  for (int i = l; i <= L; i++)
    (nl += getans(l, i - 1) * getans(i, L - 1) % 998244353) %= 998244353;
  for (int i = R; i <= r; i++)
    (nr += getans(R + 1, i) * getans(i + 1, r) % 998244353) %= 998244353;
  dp[l][r] = nl * nr % 998244353;
  for (int i = 0; i < sz - 1; i++)
    (dp[l][r] *= getans(p[mn][i] + 1, p[mn][i + 1] - 1)) %= 998244353;
  return dp[l][r];
}
int main() {
  memset(dp, -1, sizeof(dp));
  n = rd(), m = rd();
  for (int i = 1; i <= m; i++) a[i] = rd();
  tot = unique(a + 1, a + m + 1) - a - 1;
  for (int i = 1; i <= tot; i++) p[a[i]].push_back(i);
  if (tot > 2 * n) {
    printf("0\n");
    return 0;
  }
  printf("%lld\n", getans(1, tot));
  return 0;
}
