#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const ll mx=3e5;
#define ff first
#define ss second
#define pi pair<ll,ll>
#define pii pair<pi,ll>
#define memo(a,b) memset(a,b,sizeof(a))
#define pb push_back
#define MAX3(a,b,c) max(a,max(b,c))
#define MIN3(a,b,c) min(a,min(b,c))
#define V vector<ll>
#define VP vector<pi>
#define all(a) a.begin(),a.end()
#define PI acos(-1)
#define all(a) a.begin(),a.end()
#define check(a,b) ((1ll<<a)&b)
ll bigmod(ll b,ll p)
{
if(p==0)return 1;
if(p%2==0)
{
ll h=bigmod(b,p/2);
return (h*h)%mod;
}
else
{
return (b*(bigmod(b,p-1)))%mod;
}
}
void solve()
{
ll n,m;
cin>>n>>m;
map<ll,vector<ll>>xx;
ll a[n+10];
ll b[m+10];
for(ll i=1;i<=n;i++)
{
cin>>a[i];
}
ll p[n+10];
p[n+1]=1e18;
for(ll i=n;i>=1;i--)p[i]=min(a[i],p[i+1]);
for(ll i=1;i<=n;i++)
{
//if(p[i]==a[i])
xx[p[i]].pb(i);
//cout<<p[i]<<" ";
}
for(ll i=1;i<=m;i++)cin>>b[i];
for(ll i=1;i<=m;i++)
{
if(!xx[b[i]].size()||p[1]!=b[1])
{
cout<<0<<"\n";return;
}
}
ll ans=1;
for(ll i=2;i<=m;i++)
{
//cout<<xx[b[i]].back()<<" "<<xx[b[i-1]][0]<<"\n";
//if(xx[b[i]].size()==1)continue;
ans=(ans*(xx[b[i]].back()-xx[b[i]][0]+1))%mod;
}
cout<<ans<<"\n";
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//ll t;cin>>t;while(t--)
solve();
return 0;
}
1440A - Buy the String | 1658F - Juju and Binary String |
478A - Initial Bet | 981A - Antipalindrome |
365A - Good Number | 1204B - Mislove Has Lost an Array |
1409D - Decrease the Sum of Digits | 1476E - Pattern Matching |
1107A - Digits Sequence Dividing | 1348A - Phoenix and Balance |
1343B - Balanced Array | 1186A - Vus the Cossack and a Contest |
1494A - ABC String | 1606A - AB Balance |
1658C - Shinju and the Lost Permutation | 1547C - Pair Programming |
550A - Two Substrings | 797B - Odd sum |
1093A - Dice Rolling | 1360B - Honest Coach |
1399C - Boats Competition | 1609C - Complex Market Analysis |
1657E - Star MST | 1143B - Nirvana |
1285A - Mezo Playing Zoma | 919B - Perfect Number |
894A - QAQ | 1551A - Polycarp and Coins |
313A - Ilya and Bank Account | 1469A - Regular Bracket Sequence |