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;
using ll = long long;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
const ll mod = 998244353;
ll dp[2020][2020];
ll a[1234567];
template <typename T>
struct SegmentTree {
 private:
  int n;
  T E;
  vector<T> node;
  inline void updatef(T& x, T& y) { x = y; }
  inline T queryf(T& x, T& y) { return min(x, y); }

 public:
  SegmentTree(int sz, T E_) : E(E_) {
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, E);
  }
  SegmentTree(vector<T>& A, T E_) : E(E_) {
    int sz = A.size();
    n = 1;
    while (n < sz) n <<= 1;
    node.resize(2 * n - 1, E);
    for (int i = (int)(0); i < (int)(sz); ++i) node[i + n - 1] = A[i];
    for (int i = n - 2; i >= 0; --i) {
      node[i] = queryf(node[2 * i + 1], node[2 * i + 2]);
    }
  }
  void update(int k, T x) {
    k += n - 1;
    updatef(node[k], x);
    while (k > 0) {
      k = (k - 1) / 2;
      node[k] = queryf(node[2 * k + 1], node[2 * k + 2]);
    }
  }
  T get(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0) r = n;
    if (r <= a || b <= l) return E;
    if (a <= l && r <= b) return node[k];
    T xl = get(a, b, 2 * k + 1, l, (l + r) / 2);
    T xr = get(a, b, 2 * k + 2, (l + r) / 2, r);
    return queryf(xl, xr);
  }
};
SegmentTree<int> sg(1010101, inf);
vector<int> sl, sr, rev;
vector<pair<int, int>> in[555];
ll rec(int l, int r) {
  if (dp[l][r] != -1) return dp[l][r];
  if (l == r) return dp[l][r] = 1;
  int mi = sg.get(rev[l], rev[r]);
  int ml = sl[mi], mr = sr[mi];
  if (ml < l || r < mr) {
    return dp[l][r] = 0;
  }
  if (l + 1 == r) return dp[l][r] = 1;
  ll vl = 0, vr = 0;
  for (int i = (int)(l); i < (int)(ml + 1); ++i) {
    vl += rec(l, i) * rec(i, ml) % mod;
  }
  for (int i = (int)(mr + 1); i < (int)(r + 1); ++i) {
    vr += rec(mr + 1, i) * rec(i, r) % mod;
  }
  ll vm = 1;
  for (auto p : in[mi]) {
    vm *= rec(p.first, p.second);
    vm %= mod;
  }
  vl %= mod;
  vr %= mod;
  return dp[l][r] = vl * vm % mod * vr % mod;
}
int main() {
  int n, m;
  cin >> n >> m;
  sl.resize(n, inf);
  sr.resize(n, -inf);
  for (int i = (int)(0); i < (int)(m); ++i) {
    cin >> a[i];
    --a[i];
  }
  for (int i = (int)(0); i < (int)(m); ++i) {
    sg.update(i, a[i]);
    sl[a[i]] = min(sl[a[i]], i);
    sr[a[i]] = max(sr[a[i]], i);
    if (in[a[i]].size() && in[a[i]].back().second == -1) {
      in[a[i]].back().second = i;
      rev.push_back(i);
    }
    if (i < m - 1 && a[i] != a[i + 1]) {
      in[a[i]].push_back({i + 1, -1});
      rev.push_back(i + 1);
    }
  }
  for (int i = (int)(0); i < (int)(n); ++i) {
    rev.push_back(sl[i]);
    rev.push_back(sr[i]);
    if (in[i].size() && in[i].back().second == -1) in[i].pop_back();
  }
  sort(rev.begin(), rev.end());
  rev.erase(unique(rev.begin(), rev.end()), rev.end());
  for (int i = (int)(0); i < (int)(n); ++i) {
    sl[i] = lower_bound(rev.begin(), rev.end(), sl[i]) - rev.begin();
    sr[i] = lower_bound(rev.begin(), rev.end(), sr[i]) - rev.begin();
    for (auto& p : in[i]) {
      p.first = lower_bound(rev.begin(), rev.end(), p.first) - rev.begin();
      p.second = lower_bound(rev.begin(), rev.end(), p.second) - rev.begin();
    }
  }
  for (int i = (int)(0); i < (int)(2020); ++i)
    for (int j = (int)(0); j < (int)(2020); ++j) dp[i][j] = -1;
  int k = rev.size();
  if (k > 2000) {
    cout << 0 << endl;
    return 0;
  }
  rev.push_back(m);
  cout << rec(0, k) << endl;
  return 0;
}