Problem: This is the first subtask of problem F. The only differences between this and the second subtask are the constraints on the value of m and the time limit. You need to solve both subtasks in order to hack this one.

There are n+1 distinct colours in the universe, numbered 0 through n. There is a strip of paper m centimetres long initially painted with colour 0. 

Alice took a brush and painted the strip using the following process. For each i from 1 to n, in this order, she picks two integers 0 ≤ a_i < b_i ≤ m, such that the segment [a_i, b_i] is currently painted with a single colour, and repaints it with colour i. 

Alice chose the segments in such a way that each centimetre is now painted in some colour other than 0. Formally, the segment [i-1, i] is painted with colour c_i (c_i ≠ 0). Every colour other than 0 is visible on the strip.

Count the number of different pairs of sequences \\{a_i\}_{i=1}^n, \\{b_i\}_{i=1}^n that result in this configuration. 

Since this number may be large, output it modulo 998244353.

Input

The first line contains a two integers n, m (1 ≤ n ≤ 500, n = m) — the number of colours excluding the colour 0 and the length of the paper, respectively.

The second line contains m space separated integers c_1, c_2, …, c_m (1 ≤ c_i ≤ n) — the colour visible on the segment [i-1, i] after the process ends. It is guaranteed that for all j between 1 and n there is an index k such that c_k = j.

Note that since in this subtask n = m, this means that c is a permutation of integers 1 through n.

Output

Output a single integer — the number of ways Alice can perform the painting, modulo 998244353.

Examples

Input


3 3
1 2 3


Output


5


Input


7 7
4 5 1 6 2 3 7


Output


165

Note

In the first example, there are 5 ways, all depicted in the figure below. Here, 0 is white, 1 is red, 2 is green and 3 is blue.

<image>

Below is an example of a painting process that is not valid, as in the second step the segment 1 3 is not single colour, and thus may not be repainted with colour 2.

<image>

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353;
int dir[8][2] = {-1, 0, 1, 0, 0, -1, 0, 1, -1, -1, -1, 1, 1, -1, 1, 1};
template <typename S, typename T>
ostream &operator<<(ostream &os, const pair<S, T> x) {
  os << "(" << x.first << ", " << x.second << ")";
  return os;
}
template <typename S, typename T>
inline bool Min(S &a, const T &b) {
  return a > b ? a = b, true : false;
}
template <typename S, typename T>
inline bool Max(S &a, const T &b) {
  return a < b ? a = b, true : false;
}
template <typename S, typename T>
inline void Adm(S &a, const T &b) {
  a = (a + b) % MOD;
  if (a < 0) a += MOD;
}
template <typename T>
inline bool IsPri(T x) {
  if (x < 2) return false;
  for (T i = 2; i * i <= x; ++i)
    if (x % i == 0) return false;
  return true;
}
template <typename T>
inline T _Gcd(T a, T b) {
  while (b) {
    T t = b;
    b = a % b;
    a = t;
  }
  return a;
}
template <typename T>
inline int _BitCnt(T x) {
  int cnt = 0;
  while (x) ++cnt, x &= x - 1;
  return cnt;
}
inline long long Pow(long long a, long long n) {
  long long t = 1;
  a %= MOD;
  while (n > 0) {
    if (n & 1) t = t * a % MOD;
    a = a * a % MOD, n >>= 1;
  }
  return t % MOD;
}
inline int read() {
  static char buf[1000000], *p1 = buf, *p2 = buf;
  int x = false;
  char ch =
      p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
          ? EOF
          : *p1++;
  ;
  bool sgn = false;
  while (ch != '-' && (ch < '0' || ch > '9'))
    ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
  ;
  if (ch == '-')
    sgn = true,
    ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
  ;
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + (ch ^ 48),
    ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2)
             ? EOF
             : *p1++;
  ;
  return sgn ? -x : x;
}
const int N = 1010, M = 1e6 + 100;
int a[M], tot[N];
long long f[N][N];
vector<int> v[N];
long long DFS(int l, int r) {
  if (l >= r) return 1;
  if (f[l][r] >= 0) return f[l][r];
  int mi = INF, pl, pr;
  for (int i = l; i <= r; ++i)
    if (Min(mi, a[i]))
      pl = pr = i;
    else if (mi == a[i])
      pr = i;
  long long A = 0, B = 1, C = 0;
  int pre = pl;
  for (int i = pl + 1; i <= pr; ++i)
    if (a[i] == mi) B = B * DFS(pre + 1, i - 1) % MOD, pre = i;
  vector<int> cnt(501);
  int flag = 0;
  for (int i = pl; i >= l; --i) {
    if (i < pl && cnt[a[i]]++ == 0) ++flag;
    if (i < pl && cnt[a[i]] == tot[a[i]]) --flag;
    if (!flag) Adm(A, DFS(l, i - 1) * DFS(i, pl - 1));
  }
  cnt.assign(501, 0), flag = 0;
  for (int i = pr; i <= r; ++i) {
    if (i > pr && cnt[a[i]]++ == 0) ++flag;
    if (i > pr && cnt[a[i]] == tot[a[i]]) --flag;
    if (!flag) Adm(C, DFS(pr + 1, i) * DFS(i + 1, r));
  }
  return f[l][r] = A * B % MOD * C % MOD;
}
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; ++i) scanf("%d", &a[i]), v[a[i]].push_back(i);
  set<int> st;
  for (int i = 1; i <= n; ++i)
    if (!v[i].empty()) {
      auto it = st.upper_bound(v[i].front());
      if (it != st.end() && *it < v[i].back()) puts("0"), exit(0);
      for (int x : v[i]) st.insert(x);
    }
  n = 0;
  for (int i = 1; i <= m; ++i)
    if (a[i] != a[n]) ++n, a[n] = a[i], ++tot[a[i]];
  memset(f, -1, sizeof(f));
  cout << DFS(1, n) << endl;
  return 0;
}