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;
template <typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.first >> a.second;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.first << " " << a.second;
  return out;
}
template <typename T, typename T1>
T amax(T &a, T1 b) {
  if (b > a) a = b;
  return a;
}
template <typename T, typename T1>
T amin(T &a, T1 b) {
  if (b < a) a = b;
  return a;
}
const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;
template <int32_t MOD>
struct mod_field {
  int32_t val;
  mod_field(long long v = 0) {
    if (v < 0) v = v % MOD + MOD;
    if (v >= MOD) v %= MOD;
    val = v;
  }
  static int32_t mod_inv(int32_t a, int32_t m = MOD) {
    int32_t g = m, r = a, x = 0, y = 1;
    while (r != 0) {
      int32_t q = g / r;
      g %= r;
      swap(g, r);
      x -= q * y;
      swap(x, y);
    }
    return x < 0 ? x + m : x;
  }
  static unsigned fast_mod(uint64_t x, unsigned m = MOD) {
    return x % m;
    unsigned x_high = x >> 32, x_low = (unsigned)x;
    unsigned quot, rem;
    asm("divl %4\n" : "=a"(quot), "=d"(rem) : "d"(x_high), "a"(x_low), "r"(m));
    return rem;
  }
  explicit operator long long() const { return val; }
  mod_field &operator+=(const mod_field &other) {
    val += other.val;
    if (val >= MOD) val -= MOD;
    return *this;
  }
  mod_field &operator-=(const mod_field &other) {
    val -= other.val;
    if (val < 0) val += MOD;
    return *this;
  }
  mod_field &operator*=(const mod_field &other) {
    val = fast_mod((uint64_t)val * other.val);
    return *this;
  }
  mod_field &operator/=(const mod_field &other) { return *this *= other.inv(); }
  friend mod_field operator+(const mod_field &a, const mod_field &b) {
    return mod_field(a) += b;
  }
  friend mod_field operator-(const mod_field &a, const mod_field &b) {
    return mod_field(a) -= b;
  }
  friend mod_field operator*(const mod_field &a, const mod_field &b) {
    return mod_field(a) *= b;
  }
  friend mod_field operator/(const mod_field &a, const mod_field &b) {
    return mod_field(a) /= b;
  }
  mod_field &operator++() {
    val = val == MOD - 1 ? 0 : val + 1;
    return *this;
  }
  mod_field &operator--() {
    val = val == 0 ? MOD - 1 : val - 1;
    return *this;
  }
  mod_field operator++(int32_t) {
    mod_field before = *this;
    ++*this;
    return before;
  }
  mod_field operator--(int32_t) {
    mod_field before = *this;
    --*this;
    return before;
  }
  mod_field operator-() const { return val == 0 ? 0 : MOD - val; }
  bool operator==(const mod_field &other) const { return val == other.val; }
  bool operator!=(const mod_field &other) const { return val != other.val; }
  mod_field inv() const { return mod_inv(val); }
  friend ostream &operator<<(ostream &stream, const mod_field &m) {
    return stream << m.val;
  }
  friend istream &operator>>(istream &stream, mod_field &m) {
    return stream >> m.val;
  }
  mod_field pow(long long p) const {
    assert(p >= 0);
    mod_field a = *this, result = 1;
    while (p > 0) {
      if (p & 1) result *= a;
      a *= a;
      p >>= 1;
    }
    return result;
  }
};
const long long N = 1005;
const long long maxn = N;
const long long max_logn = 20;
template <typename T>
struct SparseTable {
  long long log[maxn];
  T dp[max_logn][maxn];
  T combine(T a, T b) { return min(a, b); }
  SparseTable() {
    log[1] = 0;
    for (long long i = 2; i < maxn; i++) log[i] = log[i / 2] + 1;
  }
  void build(vector<T> b) {
    long long n = b.size();
    for (long long i = 0; i < n; ++i) {
      dp[0][i] = b[i];
    }
    for (long long j = 1; j < max_logn; j++)
      for (long long i = 0; i + (1 << j) < maxn; i++)
        dp[j][i] = combine(dp[j - 1][i], dp[j - 1][i + (1 << (j - 1))]);
  }
  T query(long long l, long long r) {
    long long j = log[r - l + 1];
    return combine(dp[j][l], dp[j][r - (1 << j) + 1]);
  }
};
SparseTable<long long> sp;
mod_field<MM> dp[N][N];
long long focc[N], locc[N];
bool vis[N][N];
vector<long long> pos[N];
mod_field<MM> rec(long long l, long long r) {
  if (l > r) return 1;
  if (vis[l][r]) return dp[l][r];
  vis[l][r] = 1;
  long long fcol = sp.query(l, r);
  long long mn_idx = focc[fcol];
  long long mx_idx = locc[fcol];
  if (mn_idx < l || mx_idx > r) {
    return dp[l][r] = 0;
  }
  mod_field<MM> contx = 0;
  mod_field<MM> conty = 0;
  for (long long x = l; x <= mn_idx; ++x) {
    contx += rec(l, x - 1) * rec(x, mn_idx - 1);
  }
  for (long long y = mx_idx; y <= r; ++y) {
    conty += rec(mx_idx + 1, y) * rec(y + 1, r);
  }
  mod_field<MM> ans = contx * conty;
  if (mn_idx != mx_idx) {
    for (long long i = 1; i < (long long)((pos[fcol]).size()); ++i) {
      ans *= rec(pos[fcol][i - 1] + 1, pos[fcol][i] - 1);
    }
  }
  return dp[l][r] = ans;
}
void solve() {
  long long n, m;
  cin >> n >> m;
  vector<long long> c;
  for (long long i = 0; i < m; ++i) {
    long long x;
    cin >> x;
    c.push_back(x);
    if (i && c[(long long)((c).size()) - 1] == c[(long long)((c).size()) - 2]) {
      c.pop_back();
    }
  }
  m = (long long)((c).size());
  memset(focc, -1, sizeof focc);
  for (long long i = 0; i < m; ++i) {
    if (focc[c[i]] == -1) focc[c[i]] = i;
    locc[c[i]] = i;
    pos[c[i]].push_back(i);
  }
  for (long long i = 1; i <= n; ++i) {
  }
  for (long long i = 1; i <= n; ++i) {
    for (long long j = 1; j <= n; ++j) {
      if (focc[i] < locc[j] && focc[i] > focc[j] && locc[i] > locc[j]) {
        cout << 0;
        return;
      }
    }
  }
  __f("\"HERE\"", "HERE");
  sp.build(c);
  cout << rec(0, m - 1) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long t = 1;
  while (t--) solve();
  return 0;
}