#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*4;
  int n,m;
  int c[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    vmi.push_back(a);
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i],vmi.clear(),vmi.push_back(i);
      else if(c[i]==miv)vmi.push_back(i);
    static int mz[maxm];
    memset(mz,-1,sizeof(mz));
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv){
        if(mz[c[i]]>=0&&mz[c[i]]!=lx)return _r=0;
        mz[c[i]]=lx;
      }else lx=i;
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    {
      static int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)mr[c[i]]++,mx[c[i]]=1;
      for(int i=a,ie=mifr;i<ie;i++){
        if(!ml[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--mr[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rl+=solve(a,i)*solve(i+1,mifr-1);
      }
    }
    {
      static int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)ml[c[i]]++,mx[c[i]]=1;
      for(int i=b,ie=mito;i>mito;i--){
        if(!mr[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--ml[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rr+=solve(mito+1,i-1)*solve(i,b);
      }
    }
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    int x,xl=-1;
    int m2=0;
    for(int i=1;i<=m;i++){
      cin>>x;
      if(x!=xl)c[++m2]=x;
      xl=x;
    }
    memset(dp,-1,sizeof(dp));
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*10;
  int n,m;
  int c[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    vmi.push_back(a);
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i],vmi.clear(),vmi.push_back(i);
      else if(c[i]==miv)vmi.push_back(i);
    map<int,set<int>>mz;
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv)mz[c[i]].insert(lx);
      else lx=i;
    }
    for(const auto&[p,q]:mz){
      if(q.size()>1)return _r=0;
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    {
      map<int,int>ml,mr,mx;
      int n2=0;
      for(int i=a;i<=b;i++)mr[c[i]]++,mx[c[i]]=1;
      for(int i=a,ie=mifr;i<ie;i++){
        if(!ml[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--mr[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rl+=solve(a,i)*solve(i+1,mifr-1);
      }
    }
    {
      map<int,int>ml,mr,mx;
      int n2=0;
      for(int i=a;i<=b;i++)ml[c[i]]++,mx[c[i]]=1;
      for(int i=b,ie=mito;i>mito;i--){
        if(!mr[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--ml[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rr+=solve(mito+1,i-1)*solve(i,b);
      }
    }
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    int x,xl=-1;
    int m2=0;
    for(int i=1;i<=m;i++){
      cin>>x;
      if(x!=xl)c[++m2]=x;
      xl=x;
    }
    memset(dp,-1,sizeof(dp));
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*4;
  int n,m;
  int c[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i];
    static int mz[maxm];
    memset(mz,-1,sizeof(mz));
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv){
        if(mz[c[i]]>=0&&mz[c[i]]!=lx)return _r=0;
        mz[c[i]]=lx;
      }else lx=i,vmi.push_back(i);
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    {
      int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)if(c[i]>miv)mr[c[i]]++,mx[c[i]]=1;
      for(int i=a,ie=mifr;i<ie;i++){
        if(!ml[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--mr[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rl+=solve(a,i)*solve(i+1,mifr-1);
      }
    }
    {
      int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)if(c[i]>miv)ml[c[i]]++,mx[c[i]]=1;
      for(int i=b,ie=mito;i>mito;i--){
        if(!mr[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--ml[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rr+=solve(mito+1,i-1)*solve(i,b);
      }
    }
    return _r=rm*rl*rr;
  }
  int mi[maxn],ma[maxn];
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    int x,xl=-1;
    int m2=0;
    for(int i=1;i<=n;i++)mi[i]=m+1,ma[i]=0;
    for(int i=1;i<=m;i++){
      cin>>x;
      if(x!=xl){
        c[++m2]=x;
        mi[x]=min(mi[x],m2);
        ma[x]=max(ma[x],m2);
      }
      xl=x;
    }
    for(int i=1;i<=n;i++){
      for(int j=mi[i];j<=ma[i];j++){
        if(c[j]<i){
          cout<<0<<endl;
          return 0;
        }
      }
    }
    memset(dp,-1,sizeof(dp));
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*2;
  int n,m;
  int c[maxm];
  int d[maxm];
  typedef int number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    vmi.push_back(a);
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i],vmi.clear(),vmi.push_back(i);
      else if(c[i]==miv)vmi.push_back(i);
    map<int,set<int>>mz;
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv)mz[c[i]].insert(lx);
      else lx=i;
    }
    for(auto&[p,q]:mz){
      if(q.size()>1)return _r=0;
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    for(int i=a;i<mifr;i++)rl+=solve(a,i)*solve(i+1,mifr-1);
    for(int i=mito;i<b;i++)rr+=solve(mito+1,i)*solve(i+1,b);
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
      cin>>c[i];
    }
    int m2=0;
    for(int i=1;i<=m;){
      c[++m2]=c[i];
      int&dd=d[m2];
      dd=1;
      for(int cc=c[i++];i<=m&&c[i]==cc;i++)dd++;
    }
    memset(dp,-1,sizeof(dp));
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*10;
  int n,m;
  int c[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    vmi.push_back(a);
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i],vmi.clear(),vmi.push_back(i);
      else if(c[i]==miv)vmi.push_back(i);
    map<int,set<int>>mz;
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv)mz[c[i]].insert(lx);
      else lx=i;
    }
    for(auto&[p,q]:mz){
      if(q.size()>1)return _r=0;
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    for(int i=a;i<mifr;i++)rl+=solve(a,i)*solve(i+1,mifr-1);
    for(int i=mito;i<b;i++)rr+=solve(mito+1,i)*solve(i+1,b);
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    int x,xl=-1;
    int m2=0;
    for(int i=1;i<=m;i++){
      cin>>x;
      if(x!=xl)c[++m2]=x;
      xl=x;
    }
    memset(dp,-1,sizeof(dp));
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*4;
  int n,m;
  int c[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    vmi.push_back(a);
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i],vmi.clear(),vmi.push_back(i);
      else if(c[i]==miv)vmi.push_back(i);
    static int mz[maxm];
    memset(mz,-1,sizeof(mz));
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv){
        if(mz[c[i]]>=0&&mz[c[i]]!=lx)return _r=0;
        mz[c[i]]=lx;
      }else lx=i;
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    {
      static int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)mr[c[i]]++,mx[c[i]]=1;
      for(int i=a,ie=mifr;i<ie;i++){
        if(!ml[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--mr[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rl+=solve(a,i)*solve(i+1,mifr-1);
      }
    }
    {
      static int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)ml[c[i]]++,mx[c[i]]=1;
      for(int i=b,ie=mito;i>mito;i--){
        if(!mr[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--ml[c[i]])if(--mx[c[i]]==1)n2--;
        if(!n2)rr+=solve(mito+1,i-1)*solve(i,b);
      }
    }
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    int x,xl=-1;
    int m2=0;
    for(int i=1;i<=m;i++){
      cin>>x;
      if(x!=xl)c[++m2]=x;
      xl=x;
    }
    memset(dp,-1,sizeof(dp));
    assert(m2<maxm);
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*2;
  int n,m;
  int c[maxm];
  int d[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    vmi.push_back(a);
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i],vmi.clear(),vmi.push_back(i);
      else if(c[i]==miv)vmi.push_back(i);
    map<int,set<int>>mz;
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv)mz[c[i]].insert(lx);
      else lx=i;
    }
    for(auto&[p,q]:mz){
      if(q.size()>1)return _r=0;
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    for(int i=a;i<mifr;i++)rl+=solve(a,i)*solve(i+1,mifr-1);
    for(int i=mito;i<b;i++)rr+=solve(mito+1,i)*solve(i+1,b);
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    for(int i=1;i<=m;i++){
      cin>>c[i];
    }
    int m2=0;
    for(int i=1;i<=m;){
      c[++m2]=c[i];
      int&dd=d[m2];
      dd=1;
      for(int cc=c[i++];i<=m&&c[i]==cc;i++)dd++;
    }
    memset(dp,-1,sizeof(dp));
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


#pragma GCC optimize("O3")
#ifdef ONLINE_JUDGE
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif
#pragma GCC optimize("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#include <bits/stdc++.h>
#include <deque>
#include <type_traits>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
template<typename KeyType=int,typename Mapped=__gnu_pbds::null_type,typename Cmp_Fn=std::less<KeyType>,typename Tag=__gnu_pbds::rb_tree_tag,template<typename Const_Node_Iterator,typename Node_Iterator,typename Cmp_Fn_,typename Allocator_>class Node_Update=__gnu_pbds::tree_order_statistics_node_update,typename Allocator=std::allocator<char>>using ordered_set_t=__gnu_pbds::tree<KeyType,Mapped,Cmp_Fn,Tag,Node_Update,Allocator>;const int GRANDOM=std::chrono::high_resolution_clock::now().time_since_epoch().count();struct ghash{int operator()(int x){return std::hash<int>{}(x^GRANDOM);}};template<typename KeyType>using hash_table_t=__gnu_pbds::gp_hash_table<KeyType,int,ghash>;namespace ____{template<class T>struct like_array:is_array<T>{};template<class T,size_t N>struct like_array<array<T,N>>:true_type{};template<class T>struct like_array<vector<T>>:true_type{};template<class T>bool is_like_array(const T&a){return like_array<T>::value;}template<class T>void _R(T&x){std::cin>>x;}inline void _R(int&x){scanf("%d",&x);}inline void _R(int64_t&x){scanf("%" SCNd64,&x);}inline void _R(double&x){scanf("%lf",&x);}inline void _R(char&x){scanf(" %c",&x);}inline void _R(char*x){scanf("%s",x);}template<typename T>inline void _R(vector<T>&v,size_t ie){for(int i=0;i<ie;i++)_R(v[i]);}template<typename T>inline void _R(vector<T>&v){_R(v,v.size());}inline void R(){}template<class T,class... U>inline void R(T&head,U&... tail){_R(head);R(tail...);}template<class T>void _W(const T&x){cout<<x;}template<typename T>inline void _W(vector<T>&v,size_t ie){for(int i=0;i<ie;i++){if(i>0)_W(' ');_W(v[i]);}_W('\n');}template<typename T>inline void _W(vector<T>&v){_W(v,v.size());}template<class T>inline void _W(const vector<T>&x){for(auto i=x.begin();i!=x.end();_W(*i++))if(i!=x.cbegin())putchar(' ');}inline void W(){}template<class T,class... U>inline void W(const T&head,const U&... tail){_W(head);if(sizeof...(tail))putchar(' '),W(tail...);}template<class T,class... U>inline void WL(const T&head,const U&... tail){_W(head);putchar(sizeof...(tail)? ' ':'\n');W(tail...);}template<class T>void _RE(T&x){cin>>x;}template<class Arg,class... Args>void _RE(Arg&first,Args&... rest);void _RE(double&x){string t;_RE(t);x=stod(t);}void _RE(long double&x){string t;_RE(t);x=stold(t);}template<class T>void _RE(complex<T>&x);template<class T1,class T2>void _RE(pair<T1,T2>&p);template<class T>void _RE(vector<T>&a);template<class T,size_t SZ>void _RE(array<T,SZ>&a);template<class Arg,class... Args>void _RE(Arg&first,Args&... rest){_RE(first);_RE(rest...);}template<class T>void _RE(complex<T>&x){T a,b;_RE(a,b);x=cd(a,b);}template<class T1,class T2>void _RE(pair<T1,T2>&p){_RE(p.f,p.s);}template<class T>void _RE(vector<T>&a){for(int i=0,ie=sz(a);i<ie;i++)_RE(a[i]);}template<class T,size_t SZ>void _RE(array<T,SZ>&a){for(int i=0;i<SZ;i++)_RE(a[i]);}};
#define prec setprecision
namespace ____{typedef long long ll;typedef unsigned long long llu;template<typename T>T gcd(T m,T n){while(n){T t=m%n;m=n;n=t;};return m;}template<typename T>T exgcd(T a,T b,T&sa,T&ta){T q,r,sb=0,tb=1,sc,tc;sa=1,ta=0;if(b)do q=a/b,r=a-q*b,a=b,b=r,sc=sa-q*sb,sa=sb,sb=sc,tc=ta-q*tb,ta=tb,tb=tc;while(b);return a;}template<typename T>T mul_inv(T a,T b){T t1=a,t2=b,t3;T v1=1,v2=0,v3;T x;while(t2!=1)x=t1/t2,t3=t1-x*t2,v3=v1-x*v2,t1=t2,t2=t3,v1=v2,v2=v3;return(v2+b)%b;}template<typename T>T powmod(T a,T b,T MOD){if(b<0)return 0;T rv=1;while(b)(b%2)&&(rv=(rv*a)%MOD),a=a*a%MOD,b/=2;return rv;}template<typename T>inline T isqrt(T k){T r=sqrt((double)k)+1;while(r*r>k)r--;return r;}template<typename T>inline T icbrt(T k){T r=cbrt((double)k)+1;while(r*r*r>k)r--;return r;}template<typename T>bool mul_overflow(T&r,T a,T b){return __builtin_mul_overflow(a,b,&r);}template<ll n>struct BitSize{enum{Size=BitSize<n/2>::Size+1};};template<>struct BitSize<0>{enum{Size=1};};template<>struct BitSize<1>{enum{Size=1};};
#define BITSIZE(n) (BitSize<n>::Size)
#define TREESIZE(n) ((1<<(BitSize<n>::Size + 1))+7)
#define BITMAX(n) (BitSize<n>::Size - 1)
#define DEBUG !defined(ONLINE_JUDGE)
#define SZ(x) ((int)(x).size())
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(),(x).rend()
template<typename TH>void _dbg(const char*sdbg,TH h){cerr<<sdbg<<"="<<h<<"\n";}template<typename TH,typename... TA>void _dbg(const char*sdbg,TH h,TA... t){while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<",";_dbg(sdbg+1,t...);}
#ifdef DEBUG
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; for(auto itt: x) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
}namespace ____{template<typename T>int min_rotation(vector<T>s){int a=0,N=s.size();for(int b=0;b<N;b++)for(int i=0;i<N;i++){if(a+i==b||s[(a+i)%N]<s[(b+i)%N]){b+=max(0,i-1);break;}if(s[(a+i)%N]>s[(b+i)%N]){a=b;break;}}return a;};template<typename T>class StackGuard{private:T x;public:StackGuard(T x):x(x){}~StackGuard(){x();}};}namespace ____{template<typename T>constexpr T vh__sqrt_helper(T x,T lo,T hi){if(lo==hi)return lo;const T mid=(lo+hi+1)/2;if(x/mid<mid)return vh__sqrt_helper<T>(x,lo,mid-1);else return vh__sqrt_helper(x,mid,hi);}template<typename T>constexpr T ct_sqrt(T x){return vh__sqrt_helper<T>(x,0,x/2+1);}template<const ll MOD,typename T=ll>class FiniteField{typedef FiniteField<MOD,T>self;enum{MOD_SQRT=ct_sqrt(MOD),};public:ll val;FiniteField(ll x=0):val(normalize(x)){}inline T normalize(T x){x%=MOD;return normalize2(x);}inline T normalize2(T x){if(x<0)x+=MOD;if(x>=MOD)x-=MOD;return x;}self&operator=(const self&o){val=o.val;return*this;}template<typename NT>self&operator=(const NT o){val=normalize(o);return*this;}self&operator+=(const self&o){val=normalize2(val+o.val);return*this;}template<typename NT>self&operator+=(const NT o){val=normalize(val+(o));return*this;}self&operator-=(const self&o){val=normalize2(val-o.val);return*this;}template<typename NT>self&operator-=(const NT o){val=normalize(val-(o));return*this;}self&operator*=(const self&o){return*this*=o.val;}template<typename NT>self&operator*=(const NT o){val=normalize(val*o);return*this;}self operator+(const self&o){return self(normalize2(val+o.val));}template<typename NT>self operator+(const NT o){return self(normalize2(val+(o)));}self operator-(const self&o){return self(normalize2(val-o.val));}template<typename NT>self operator-(const NT o){return self(normalize2(val-(o)));}self operator-(){return self(normalize(-val));}self operator*(const self&o){return self(normalize(val*o.val));}template<typename NT>self operator*(const NT o){return self(normalize(val*(o)));}self operator^(ll b){if(b<0)return 0;self a=*this;self rv=1;while(b)(b%2)&&(rv*=a),a*=a,b/=2;return rv;}operator T()const{return val;}template<typename NT,typename NT2>self log(NT base,NT2 base_inv){static const ll magic=MOD_SQRT+3;base=normalize(base);map<ll,ll>lg;ll x=1;for(int i=0;i<magic;i++)lg[x]=i,x=x*base%MOD;ll step=self(base_inv)^magic;x=val;for(int i=0;i<magic;i++){auto it=lg.find(x);if(it!=lg.end()){return self(magic*i+it->second);}x=(x*step)%MOD;}return-1;}};};namespace ____{};
namespace ____{
  const ll MOD=998244353;
  const int maxn=505;
  const int maxm=maxn*4;
  int n,m;
  int c[maxm];
  typedef FiniteField<MOD,ll>number_t;
  ll dp[maxm][maxm];
  number_t solve(int a,int b){
    if(a>=b)return 1;
    ll&_r=dp[a][b];
    if(_r>=0)return _r;
    vector<int>vmi;
    int miv=c[a];
    for(int i=a;i<=b;i++)
      if(c[i]<miv)miv=c[i];
    static int mz[maxm];
    memset(mz,-1,sizeof(mz));
    int lx=a-1;
    for(int i=a;i<=b;i++){
      if(c[i]>miv){
        if(mz[c[i]]>=0&&mz[c[i]]!=lx)return _r=0;
        mz[c[i]]=lx;
      }else lx=i,vmi.push_back(i);
    }
    number_t rm=1;
    for(int i=1,ie=vmi.size();i<ie;i++)rm*=solve(vmi[i-1]+1,vmi[i]-1);
    int mifr=*vmi.begin();
    int mito=*vmi.rbegin();
    number_t rl=solve(a,mifr-1),rr=solve(mito+1,b);
    {
      int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)if(c[i]>miv)mr[c[i]]++,mx[c[i]]=1;
      for(int i=a,ie=mifr;i<ie;i++){
        assert(c[i]>miv);
        if(!ml[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--mr[c[i]])if(--mx[c[i]]==1)n2--;
        assert(n2>=0);
        if(!n2)rl+=solve(a,i)*solve(i+1,mifr-1);
      }
    }
    {
      int ml[maxm],mr[maxm],mx[maxm];
      memset(ml,0,sizeof(ml));
      memset(mr,0,sizeof(mr));
      memset(mx,0,sizeof(mx));
      int n2=0;
      for(int i=a;i<=b;i++)if(c[i]>miv)ml[c[i]]++,mx[c[i]]=1;
      for(int i=b,ie=mito;i>mito;i--){
        assert(c[i]>miv);
        if(!mr[c[i]]++)if(++mx[c[i]]==2)n2++;
        if(!--ml[c[i]])if(--mx[c[i]]==1)n2--;
        assert(n2>=0);
        if(!n2)rr+=solve(mito+1,i-1)*solve(i,b);
      }
    }
    return _r=rm*rl*rr;
  }
  int main(){
    srand(GRANDOM);
    cin>>n>>m;
    int x,xl=-1;
    int m2=0;
    for(int i=1;i<=m;i++){
      cin>>x;
      if(x!=xl)c[++m2]=x;
      xl=x;
    }
    memset(dp,-1,sizeof(dp));
    assert(m2<maxm);
    cout<<solve(1,m2)<<endl;
    return 0;
  }
};
int main(int argc,char*argv[]){
  std::cin.sync_with_stdio(false);std::cin.tie(nullptr);
  return ____::main();
}


def solver():
    n = int(input())
    vals = list(map(int, input().split()))
    types = list(map(int, input().split()))

    temp = []
    for i, j in zip(vals, type):
        temp.append((i, j))

    temp.sort(key=lambda x: x[0])

    count_zero = types.count(0)
    diff = 0

    for i in range(n):
        if(temp[i][0] != vals[i]):
            diff += 1

    if diff == 0:
        return ("Yes")

    else:
        if diff == 0 or count_zero == n:
            return ("No")
        else:
            return ("Yes")

for _ in range(int(input())):

    print(solver())


def solver():
    n = int(input())
    vals = list(map(int, input().split()))
    type = list(map(int, input().split()))

    temp = []
    for i, j in zip(vals, type):
        temp.append((i, j))

    temp.sort(key=lambda x: x[0])

    count_zero = type.count(0)
    diff = 0

    for i in range(n):
        if(temp[i][0] != vals[i]):
            diff = diff + 1

    if diff == 0:
        return ("Yes")

    else:
        if diff == 0 or count_zero == n:
            return ("No")
        else:
            return ("Yes")

for _ in range(int(input())):

    print(solver())


def solver():
    pass

for _ in range(int(input())):

    n = int(input())
    vals = list(map(int, input().split()))
    types = list(map(int, input().split()))

    temp = []
    for i, j in zip(vals, types):
        temp.append((i, j))

    temp.sort(key=lambda x: x[0])

    count_zero = types.count(0)
    diff = 0

    for i in range(n):
        if(temp[i][0] != vals[i]):
            diff += 1

    if diff == 0:
        print("Yes")

    else:
        if diff == 0 or count_zero == n:
            print("No")
        else:
            print("Yes")


def solver():
    vals = list(map(int, input().split()))
    type = list(map(int, input().split()))

    temp = []
    for i, j in zip(vals, type):
        temp.append((i, j))

    temp.sort(key=lambda x: x[0])

    count_zero = type.count(0)
    diff = 0

    for i in range(n):
        if(temp[i][0] != vals[i]):
            diff = diff + 1

    if diff == 0:
        return ("Yes")

    else:
        if diff == 0 or count_zero == n:
            return ("No")
        else:
            return ("Yes")

for _ in range(int(input())):
    n = int(input())

    print(solver())


def solver():
    pass

for _ in range(int(input())):

    n = int(input())
    vals = list(map(int, input().split()))
    types = list(map(int, input().split()))

    temp = []
    for i, j in zip(vals, types):
        temp.append((i, j))

    temp.sort(key=lambda x: x[0])

    count_zero = types.count(0)
    diff = 0

    for i in range(n):
        if(temp[i][0] != vals[i]):
            diff += 1

    if diff == 0:
        print("Yes")

    else:
        if count_zero == 0 or count_zero == n:
            print("No")
        else:
            print("Yes")


def solver():
    n = int(input())
    vals = list(map(int, input().split()))
    types = list(map(int, input().split()))

    temp = []
    for i, j in zip(vals, types):
        temp.append((i, j))

    temp.sort(key=lambda x: x[0])

    count_zero = types.count(0)
    diff = 0

    for i in range(n):
        if(temp[i][0] != vals[i]):
            diff += 1

    if diff == 0:
        return ("Yes")

    else:
        if diff == 0 or count_zero == n:
            return ("No")
        else:
            return ("Yes")

for _ in range(int(input())):

    print(solver())


def is_sorted(a):
    for i in range(len(a)-1):
        if a[i] > a[i + 1]:
            return False
    return True

def solve(n, a, b):
    for i in range(n - 1):
        for j in range(i + 1, n):
            if b[i] != b[j] and a[i] > a[j]:
                a[i], a[j] = a[j], a[i]
                b[i], b[j] = b[j], b[i]
    return "yes" if is_sorted(a) else "no"

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(solve(n, a, b))


def is_sorted(a):
    for i in range(len(a)-1):
        if a[i] > a[i + 1]:
            return False
    return True

def solve(n, a, b):
    for i in range(n - 1):
        for j in range(i + 1, n):
            if a[i] > a[j]:
                if b[i] != b[j]:
                    a[i], a[j] = a[j], a[i]
                    b[i], b[j] = b[j], b[i]
                else:
                    for k in range(i - 1, 0, -1):
                        if b[k] != b[j]:
                            if a[j] > a[k]:
                                a[k], a[i], a[j] = a[k], a[j], a[i]
                                b[k], b[i], b[j] = b[k], b[j], b[i]
                            else:
                                a[k], a[i], a[j] = a[j], a[k], a[i]
                                b[k], b[i], b[j] = b[j], b[k], b[i]

    return "yes" if is_sorted(a) else "no"

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(solve(n, a, b))


def is_sorted(a):
    for i in range(len(a)-1):
        if a[i] > a[i + 1]:
            return False
    return True

def solve(n, a, b):
    for i in range(n - 1):
        for j in range(i + 1, n):
            if b[i] != b[j] and a[i] >= a[j]:
                a[i], a[j] = a[j], a[i]
                b[i], b[j] = b[j], b[i]

    return "yes" if is_sorted(a) else "no"

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(solve(n, a, b))


def is_sorted(a):
    for i in range(len(a)-1):
        if a[i] > a[i + 1]:
            return False
    return True

def solve(n, a, b):
    return "yes" if (0 in b and 1 in b) or is_sorted(a) else "no"

for _ in range(int(input())):
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))
    print(solve(n, a, b))


t=int(input())
while t:
    t-=1
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    li=[]
    mi=[]
    result=0
    for i in a:
        li.append(i)
        mi.append(i)
    li.sort()
    if li==a:
        print('YES')
    else:
        for i in range(len(li)):
            if mi.index(li[i])==i:
                pass
            else:
                if b[i]!=b[mi.index(li[i])]:
                    a[i]=mi[mi.index(li[i])]
        for i in range(len(a)-1):
            if a[i]<=a[i+1]:
                result=1
            else:
                result=0
                break
        if result:
            print('YES')
        else:
            print('NO')


t=int(input())
while t:
    t-=1
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    li=[]
    mi=[]
    for i in a:
        li.append(i)
    li.sort()
    if li==a:
        print('YES')
    else:
        for i in range(len(li)):
            if a.index(li[i])==i:
                pass
            else:
                if b[i]!=b[a.index(li[i])]:
                    mi.append(a[a.index(li[i])])
                    print('YES')
                    break
        else:
            print('NO')


import copy
for _ in range(int(input())):
    n=int(input())
    arr1=list(map(int,input().split()))
    arr2=list(map(int,input().split()))
    sort=True
    zeros,ones=False,False
    for i in range(1,n):
        if(arr1[i]<arr1[i-1]):
            sort=False
    for i in range(n):
        if(arr2[i]==1):
            ones=True
        else:
            zeros=True
    if(zeros and ones):
        print("YES")
    elif(sort):
        print("YES")
    else:
        print("NO")


t=int(input())
while t:
    t-=1
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    li=[]
    mi=[]
    result=0
    for i in a:
        li.append(i)
        mi.append(i)
    li.sort()
    if li==a:
        print('Yes')
    else:
        for i in range(len(li)):
            if mi.index(li[i])==i:
                pass
            else:
                if b[i]!=b[mi.index(li[i])]:
                    a[i]=mi[mi.index(li[i])]
        for i in range(len(a)-1):
            if a[i]<=a[i+1]:
                result=1
            else:
                print('No')
                result=0
                break
        if result:
            print('Yes')


t=int(input())
while t:
    t-=1
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    li=[]
    mi=[]
    result=0
    for i in a:
        li.append(i)
        mi.append(i)
    li.sort()
    if li==a:
        print('Yes')
    else:
        for i in range(len(li)):
            if mi.index(li[i])==i:
                pass
            else:
                if b[i]!=b[mi.index(li[i])]:
                    a[i]=mi[mi.index(li[i])]
        for i in range(len(a)-1):
            if a[i]<=a[i+1]:
                result=1
            else:
                result=0
                break
        if result:
            print('Yes')
        else:
            print('No')


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    flag=0
    for i in range(n):
        j=i+1
        min1=a[i]
        while(j<n):
            if(a[j]<min1):
                if(b[i]==b[j]):
                    flag=1
                    break
            elif(a[j]==min1):
                if(b[i]==b[j] and a[j-1]!=min1):
                    flag=1
                    break
            j+=1
    if(flag==1):
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a1=a[:]
    a1.sort()
    t1=b.count(0)
    if(t1==n or t1==0):
        if(a==a1):
            print("Yes")
        else:
            print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    flag=0
    for i in range(n):
        min1=a[i]
        j=i+1
        j1=i
        while(j<n):
            if(a[j]<min1):
                min1=a[j]
                j1=j
            j+=1
        if(min1==a[i]):
            pass
        else:
            if(b[j1]==b[i]):
                flag=1
                break
            else:
                a[i],a[j1]=a[j1],a[i]
                b[i],b[j1]=b[j1],b[i]
    if(flag==1):
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    flag=0
    for i in range(n):
        j=i+1
        min1=a[i]
        while(j<n):
            if(a[j]<min1 and b[j]==b[i]):
                flag=1
                break
            j+=1
    if(flag==1):
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    flag=0
    for i in range(n):
        min1=a[i]
        j=i+1
        j1=i
        flag1=0
        while(j<n):
            if(a[j]<min1):
                j1=j
                flag1=1
            j+=1
        if(flag1==1):
            if(b[i]==b[j1]):
                flag=1
                break
            else:
                a[i],a[j1]=a[j1],a[i]
                b[i],b[j1]=b[j1],b[i]
        else:
            pass
    if(flag==1):
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    flag=0
    for i in range(n):
        min1=a[i]
        j=i
        while(a[j]!=a[i]):
            j+=1
        while(j<n):
            if(a[j]<min1):
                if(b[i]==b[j]):
                    flag=1
                    break
            j+=1
    if(flag==1):
        print("No")
    else:
        print("Yes")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    a1=a[:]
    a1.sort()
    for i in range(n):
        min1=a[i]
        j=i+1
        j1=i
        t3=b[i]
        while(j<n):
            if(a[j]<=min1 and b[i]!=b[j]):
                j1=j
                min1=a[j]
                t3=b[j]
            j+=1
        t1=a[i]
        t2=b[i]
        a[i]=min1
        a[j1]=t1
        b[j1]=t2
        b[i]=t3
    if(a1==a):
        print("Yes")
    else:
        print("No")


t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    b=list(map(int,input().split()))
    flag=0
    for i in range(n):
        min1=a[i]
        j1=i
        j=i+1
        while(j<n):
            if(a[j]<min1):
                if(b[i]==b[j]):
                    flag=1
                    break
                else:
                    min1=a[j]
                    j1=j
            j+=1
        if(min1==a[i]):
            pass
        else:
            min1,a[i]=a[i],min1
            b[i],b[j1]=b[j1],b[i]
    if(flag==1):
        print("No")
    else:
        print("Yes")


def sorti(arr,typ_ar):
	n=len(arr)
	for i in range(n):
		for j in range(0,n-i-1):
			if arr[j]>arr[j+1] and typ_ar[j]!=typ_ar[j+1]:
				arr[j],arr[j+1]=arr[j+1],arr[j]
				typ_ar[j],typ_ar[j+1]=typ_ar[j+1],typ_ar[j]
	if (arr==sorted(arr)):
		return ("Yes")
	else:
		return ("No")

t=int(input())
for i in range(t):
	n=int(input());
	arr = [int(i) for i in input().split()][:n]
	ty_ar=[int(i) for i in input().split()][:n]
	if (arr==sorted(arr)):
		print("Yes")
	else:
		print(sorti(arr,ty_ar))


def sorti(arr,typ_ar):
	n=len(arr)
	for i in range(n):
		for j in range(i,n):
			if arr[i]>arr[j] and typ_ar[i]!=typ_ar[j]:
				arr[i],arr[j]=arr[j],arr[i]
				typ_ar[i],typ_ar[j]=typ_ar[j],typ_ar[i]
	if (arr==sorted(arr)):
		return ("Yes")
	else:
		return ("No")

t=int(input())
for i in range(t):
	n=int(input());
	arr = [int(i) for i in input().split()][:n]
	ty_ar=[int(i) for i in input().split()][:n]
	if (arr==sorted(arr)):
		print("Yes")
	else:
		print(sorti(arr,ty_ar))


t=int(input())
for i in range(t):
	n=int(input());
	arr = [int(i) for i in input().split()][:n]
	ty_ar=[int(i) for i in input().split()][:n]
	if (arr==sorted(arr)):
		print("Yes")
	else:
		c_1=0
		c_0=0
		for i in range(len(arr)):
			if ty_ar[i]==0:
				c_0+=1
			if ty_ar[i]==1:
				c_1+=1
		if (c_1>=1 and c_0>=1):
			print("yes")
		else:
			print("no")


t=int(input())

for _ in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    typarr=list(map(int,input().split()))

    for i in range(1,n):
        for j in range(i):
            if(arr[i]<arr[j] and typarr[i]!=typarr[j]):
                temp=arr[i]
                arr[i]=arr[j]
                arr[j]=arr[i]

                temp1=typarr[i]
                typarr[i]=typarr[j]
                typarr[j]=temp1

    st=0
    for i in range(n-1):
        if(arr[i]>arr[i+1]):
            print("No")
            st+=1
            break
    if(st==0):
        print("Yes")


t=int(input())

def search(num,lst,t):

    for i in lst:
        if(t==1):
            if(i>num):
                return 1
        else:
            if(i<num):
                return 1
    return 0

for _ in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    typarr=list(map(int,input().split()))
    lst1=[]
    lst0=[]
    for i in range(n):
        if(typarr[i]==0):
            lst0.append(arr[i])
        else:
            lst1.append(arr[i])
    i=1

    while(i<n):
        z=0
        check=0
        for j in range(i):
            if(arr[i]<arr[j] and typarr[i]!=typarr[j]):
                temp=arr[i]
                arr[i]=arr[j]
                arr[j]=temp

                temp1=typarr[i]
                typarr[i]=typarr[j]
                typarr[j]=temp1
                i=1
                check=1
                break

            elif(arr[i]<arr[j] and typarr[i]==typarr[j]):
                if(typarr[i]==0):
                    check=search(arr[j],lst1,1)
                    if(not check):
                        check=search(arr[j],lst1,0)
                else:
                    check=search(arr[j],lst0,1)
                    if(not check):
                        check=search(arr[j],lst0,0)

                if(check):
                    temp = arr[i]
                    arr[i] = arr[j]
                    arr[j] = temp

                    temp1 = typarr[i]
                    typarr[i] = typarr[j]
                    typarr[j] = temp1
                    i=1
                    check=1
                    break
                else:
                    z=1
                    print("No")
                    break

        if(z==1):
            break
        if(check==0):
            i+=1
    if(i>=n):
        print("Yes")


t=int(input())

def search(num,lst):
    for i in lst:
        if(i>num):
            return 1
    return 0

for _ in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    typarr=list(map(int,input().split()))
    lst1=[]
    lst0=[]
    for i in range(n):
        if(typarr[i]==0):
            lst0.append(arr[i])
        else:
            lst1.append(arr[i])
    i=1

    while(i<n):
        z=0
        check=0
        for j in range(i):
            if(arr[i]<arr[j] and typarr[i]!=typarr[j]):
                temp=arr[i]
                arr[i]=arr[j]
                arr[j]=temp

                temp1=typarr[i]
                typarr[i]=typarr[j]
                typarr[j]=temp1
                i=1
                check=1
                break

            elif(arr[i]<arr[j] and typarr[i]==typarr[j]):
                if(typarr[i]==0):
                    check=search(arr[j],lst1)
                else:
                    check=search(arr[j],lst0)

                if(check):
                    temp = arr[i]
                    arr[i] = arr[j]
                    arr[j] = temp

                    temp1 = typarr[i]
                    typarr[i] = typarr[j]
                    typarr[j] = temp1
                    i=1
                    check=1
                    break
                else:
                    z=1
                    print("No")
                    break

        if(z==1):
            break
        if(check==0):
            i+=1
    if(i>=n):
        print("Yes")


t=int(input())
def search(num1,num2,lst):

    for i in lst:
        if(i>num1 or i<num2):
            return 1
    return 0

for _ in range(t):
    n=int(input())

    arr=list(map(int,input().split()))
    typarr=list(map(int,input().split()))
    typ1=set(typarr)
    if(len(typ1)==2 or arr==sorted(arr)):
        print("Yes")
    else:
        print("No")


def solve():
    for _ in range(int(input())):
        n=int(input())
        la=list(map(int,input().strip().split()))
        a=la[:]
        a.sort()
        lb=list(map(int,input().strip().split()))
        if lb.count(1)==0 or lb.count(0)==0:
            if a==la:
                print("Yes")
            else:
                print("No")
            continue
        for i in range(n-1):
            for j in range(0,n-1-i):
                if la[j]>la[j+1] and lb[j]!=lb[j+1]:
                    la[j],la[j+1]=la[j+1],la[j]
                    lb[j],lb[j+1]=lb[j+1],lb[j]
        if la==a:
            print("Yes")
        else:
            print("No")
if __name__=="__main__":
    solve()


def  solve():
    for _ in range(int(input())):
        n=int(input())
        la=list(map(int,input().strip().split()))
        a=la[:]
        a.sort()
        lb=list(map(int,input().strip().split()))
        c0=lb.count(0)
        c1=lb.count(1)
        if lb.count(1)==0 or lb.count(0)==0:
            if a==la:
                print("Yes")
            else:
                print("No")
            continue
        if abs(c0-c1)<=1:
            print("Yes")
        else:
            print("No")
if __name__=="__main__":
    solve()


def solve():
    for _ in range(int(input())):
        n=int(input())
        la=list(map(int,input().strip().split()))
        a=la[:]
        a.sort()
        lb=list(map(int,input().strip().split()))
        if lb.count(1)==0 or lb.count(0)==0:
            if a==la:
                print("Yes")
            else:
                print("No")
            continue
        elif l.count(1)>0 and l.count(0)>0:
            print("Yes")
if __name__=="__main__":
    solve()


def solve():
    for _ in range(int(input())):
        n=int(input())
        la=list(map(int,input().strip().split()))
        a=la[:]
        a.sort()
        lb=list(map(int,input().strip().split()))
        if lb.count(1)==0 or lb.count(0)==0:
            print("No")
            continue
        for i in range(n-1):
            for j in range(0,n-1-i):
                if la[j]>la[j+1] and lb[j]!=lb[j+1]:
                    la[j],la[j+1]=la[j+1],la[j]
                    lb[j],lb[j+1]=lb[j+1],lb[j]
        if la==a:
            print("Yes")
        else:
            print("No")
if __name__=="__main__":
    solve()


def solve():
    for _ in range(int(input())):
        n=int(input())
        la=list(map(int,input().strip().split()))
        a=la[:]
        a.sort()
        lb=list(map(int,input().strip().split()))
        if lb.count(1)==0 or lb.count(0)==0:
            if a==la:
                print("Yes")
            else:
                print("No")
            continue
        elif lb.count(1)>0 and lb.count(0)>0:
            print("Yes")
if __name__=="__main__":
    solve()


is_sorted = lambda x: all(x[i] <= x[i+1] for i in range(len(x)-1))

def is_possible(a, b):
	# Check by simulating selection sort
	count = len(a)
	for i in range(count):
		min_idx = i
		for j in range(i+1, count):
			if a[min_idx] >= a[j] and b[j] != b[min_idx]:
				min_idx = j
		a[i], a[min_idx] = a[min_idx], a[i]
		b[i], b[min_idx] = b[min_idx], b[i]

	return is_sorted(a)

for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = is_sorted(a) or is_possible(a, b)

	print(['NO', 'YES'][ans])


is_sorted = lambda x: all(x[i] <= x[i+1] for i in range(len(x)-1))

def is_possible(a, b):
	# Check by simulating bubble sort
	count = len(a)
	for i in range(count):
		for j in range(0, count-i-1):
			if a[j] > a[j+1] and b[j] != b[j+1]:
				a[j], a[j+1] = a[j+1], a[j]
				b[j], b[j+1] = b[j+1], b[j]
	return is_sorted(a)

for _ in range(int(input())):
	n = int(input())
	a = list(map(int, input().split()))
	b = list(map(int, input().split()))
	ans = is_sorted(a) or is_possible(a, b)

	print(['NO', 'YES'][ans])


MI = lambda: map(int, input().split())

for _ in range(int(input())):
	n, a, b = int(input()), list(MI()), list(MI())
	# It is unpossible if 0s or 1s don't exist or
	# equal to n and the array is not sorted
	ans = b.count(0) in [0, n] and a != sorted(a)
	print(['NO', 'YES'][not ans])


def solve():
    n=int(input())
    a=[int(x) for x in input().split()]
    b=[int(x) for x in input().split()]
    li=list(a)
    d={}
    for i in range(n):
        if d.get(a[i],0)==0:
            d[a[i]]=[i]
        else:
            temp=d[a[i]]
            temp.append(i)
            d[a[i]]=temp
    li.sort()

    for i in range(n):
        if a[i]!=li[i]:
            x=b[i]
            temp=d[li[i]]

            br=False
            for ele in temp:
                if b[ele]!=x:
                    note=a[ele]
                    pos=ele
                    note2=a[i]
                    pos2=i
                    a[i],a[ele]=a[ele],a[i]
                    b[i],b[ele]=b[ele],b[i]
                    br=True
                    break
            else:
                print('No')
                return
            if br==True:
                temp.remove(pos)
                temp.append(pos2)
                d[note]=temp
                t=d[note2]
                t.remove(pos2)
                t.append(pos)
                d[note2]=t

    else:
        print('Yes')
        return

t=int(input())
for i in range(t):
    solve()


def solve():
    n=int(input())
    a=[int(x) for x in input().split()]
    b=[int(x) for x in input().split()]
    c0,c1=0,0
    for i in range(n):
        if b[i]==0:
            c0+=1
        elif b[i]==1:
            c1+=1
    if c0==n or c1==n:
        li=list(a)
        li.sort()
        if a==li:
            print('Yes')
        else:
            print('No')
    else:
        print('Yes')

t=int(input())
for i in range(t):
    solve()