#include<bits/stdc++.h> using namespace std; #define N 300005 #define mod 998244353 #define ll long long int n,a[N],m,b[N],c[N],d[N]; int main(){ cin>>n>>m; for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++)scanf("%d",&b[i]); stack<int>stk; for(int i=1;i<=n;i++){ if(stk.size()==0){stk.push(i);continue;} while(stk.size() && a[stk.top()]>=a[i]) stk.pop(); stk.push(i); } int k=stk.size(); for(int i=k;i>=1;i--){ c[i]=stk.top();stk.pop(); } if(a[c[1]]!=b[1]){puts("0");return 0;} int ans=1,p=1; for(int i=2;i<=m;i++){ while(a[c[p]]<b[i] && p<=k)p++; if(a[c[p]]>b[i] || p==k+1){puts("0");return 0;} int pos=c[p]; while(a[pos]>=b[i] && pos>0)pos--; ans=1ll*ans*(c[p]-pos)%mod; } cout<<ans<<' '; }