#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
using ll = long long;
using db = long double;
using ii = pair<int, int>;
const int N = 3e5 + 5, LG = 19, MOD = 998244353;
const int SQ = 225;
const long double EPS = 1e-7;
int dp[1005][1005];
int col[1005];
vector<int> go[505];
int solve(int i, int j) {
  if (i > j) return 1;
  int &ret = dp[i][j];
  if (~ret) return ret;
  int val = *min_element(col + i, col + j + 1);
  int lft = 0, rt = 0, idx;
  ret = 0;
  idx = go[val].back();
  if (idx > j) return ret;
  for (int k = idx; k < j + 1; k++)
    rt = (rt + solve(idx + 1, k) * 1ll * solve(k + 1, j)) % MOD;
  idx = go[val][0];
  if (idx < i) return ret;
  for (int k = i; k < idx + 1; k++)
    lft = (lft + solve(i, k - 1) * 1ll * solve(k, idx - 1)) % MOD;
  ret = 1ll * rt * lft % MOD;
  for (int k = 1; k < go[val].size(); k++)
    ret = 1ll * ret * solve(go[val][k - 1] + 1, go[val][k] - 1) % MOD;
  return ret;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  int ptr = 0;
  for (int _ = 0; _ < m; _++) {
    cin >> col[ptr++];
    if (ptr > 1 && col[ptr - 1] == col[ptr - 2]) --ptr;
    if (ptr > n + n) {
      cout << 0 << '\n';
      return 0;
    }
  }
  m = ptr;
  for (int i = 0; i < m; i++) go[col[i]].push_back(i);
  memset(dp, -1, sizeof dp);
  cout << solve(0, m - 1) << '\n';
  return 0;
}
