#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int n, m, t, c[1001], first[500], last[500];
long long dp[1000][1000];
long long f(int l, int r) {
  if (l > r) return 1;
  if (dp[l][r] != -1) return dp[l][r];
  int col = n;
  for (int i = l; i <= r; i++) col = min(col, c[i]);
  if (first[col] < l || r < last[col]) return 0;
  long long s1 = 0, s2 = 0;
  for (int a = l; a <= first[col]; a++)
    s1 = (s1 + f(l, a - 1) * f(a, first[col] - 1)) % mod;
  for (int b = last[col]; b <= r; b++)
    s2 = (s2 + f(last[col] + 1, b) * f(b + 1, r)) % mod;
  dp[l][r] = s1 * s2 % mod;
  int x = first[col];
  for (int i = first[col] + 1; i <= last[col]; i++) {
    if (c[i] != col) continue;
    dp[l][r] = (dp[l][r] * f(x + 1, i - 1)) % mod;
    x = i;
  }
  return dp[l][r];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> t;
  for (int i = 0; i < t; i++) {
    cin >> c[m];
    c[m]--;
    if (m == 0 || c[m - 1] != c[m]) m++;
    if (m > 2 * n) {
      cout << "0\n";
      return 0;
    }
  }
  for (int i = 0; i < m; i++) last[c[i]] = i;
  for (int i = m - 1; 0 <= i; i--) first[c[i]] = i;
  for (int i = 0; i < m; i++)
    for (int j = 0; j < m; j++) dp[i][j] = -1;
  cout << f(0, m - 1) << "\n";
  return 0;
}
