#include<iostream>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdio>
#include<stack>
#include<queue>
#include<stdlib.h>
#include<vector>
#include<sstream>
#include<iomanip>
using namespace std;
int main()
{
	int t,c;
	cin >> t;
	int* a = new int[t];
	int* b = new int[t];
	for (int i = 0; i < t; i++)
	{
		cin >> *(a+i) >> *(b+i);
		if (*(a + i) > * (b + i))
		{
			c = *(a + i);
			*(a + i) = *(b + i);
			*(b + i) = c;
		}
		if (*(a + i) == 1||2**(a+i)<=*(b+i))
		{
			cout << "NO" << "\n";
			continue;
		}
		for (int j = 2 * *(a + i); j > *(b+i); j--)
		{
			int n = 0;
			for (int k = *(a + i); k <= *(b + i); k++)
			{
				if (k % j >= (j + 1) / 2)
				{
					n = n + 1;
				}
			}
			if (n == *(b + i) - *(a + i) + 1)
			{
				cout << "YES" << endl;
				break;
			}
			else if(j== *(b + i)+1)cout << "NO" << endl;
		}
	}
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#define endl "\n"

#define ll long long
using namespace std;

int main() {

	int t;
	cin >> t;
	ll l, r;
	while (t--) {
		cin >> l >> r;
		if (r % l != 0) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#define endl "\n"

#define ll long long
using namespace std;

int main() {

	int t;
	cin >> t;
	ll l, r;
	while (t--) {
		cin >> l >> r;
		if (l != 1) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#define endl "\n"

#define ll long long
using namespace std;

int main() {

	int t;
	cin >> t;
	ll l, r;
	while (t--) {
		cin >> l >> r;
		if (l % r == 1) {
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl;
		}

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#define endl "\n"

#define ll long long
using namespace std;

int main() {

	int t;
	cin >> t;
	ll l, r;
	while (t--) {
		cin >> l >> r;
		if (r < l * 2) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#define endl "\n"

#define ll long long
using namespace std;

int main() {

	int t;
	cin >> t;
	ll l, r;
	while (t--) {
		cin >> l >> r;
		if (l == 1) {
			cout << "NO" << endl;
		}
		else {
			cout << "YES" << endl;
		}

	}

	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <vector>
#include <map>
#include <cmath>
#include <numeric>
#include<cctype>
#include<deque>
#define endl "\n"

#define ll long long
using namespace std;

int main() {

	int t;
	cin >> t;
	ll l, r;
	while (t--) {
		cin >> l >> r;
		if (l >= 3) {
			cout << "YES" << endl;
		}
		else {
			cout << "NO" << endl;
		}
	}

	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t-- > 0)
	{
		int l, r;
		cin >> l >> r;
		int a = r + 1;

		int upakMin = l / a;
		int upakMax = r / a;

		int pieceMin = l % a;
		int pieceMax = r % a;

		if (pieceMin >= (double)a / 2)
		{
			pieceMin = 0;
			upakMin++;
		}
		if (pieceMax >= (double)a / 2)
		{
			pieceMax = 0;
			upakMax++;
		}

		if (upakMin * a + pieceMin > r && upakMax * a + pieceMax > r)
			cout << "YES\n";
		else
			cout << "NO\n";
	}
}


#include <iostream>

int main()
{
	int t;
	cin >> t;
	while (t-- > 0)
	{
		int l, r;
		cin >> l >> r;
		int a = r + 1;

		int upakMin = l / a;
		int upakMax = r / a;

		int pieceMin = l % a;
		int pieceMax = r % a;

		if (pieceMin > a / 2)
		{
			pieceMin = 0;
			upakMin++;
		}
		if (pieceMax > a / 2)
		{
			pieceMax = 0;
			upakMax++;
		}

		if (upakMin * a + pieceMin > r && upakMax * a + pieceMax > r)
			cout << "YES";
		else
			cout << "NO";
	}
}


#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t-- > 0)
	{
		int l, r;
		cin >> l >> r;
		int a = r + 1;

		int upakMin = l / a;
		int upakMax = r / a;

		int pieceMin = l % a;
		int pieceMax = r % a;

		if (pieceMin > a / 2)
		{
			pieceMin = 0;
			upakMin++;
		}
		if (pieceMax > a / 2)
		{
			pieceMax = 0;
			upakMax++;
		}

		if (upakMin * a + pieceMin > r && upakMax * a + pieceMax > r)
			cout << "YES";
		else
			cout << "NO";
	}
}


#include <iostream>
using namespace std;

int main()
{
	int t;
	cin >> t;
	while (t-- > 0)
	{
		int l, r;
		cin >> l >> r;
		int a = r + 1;

		int upakMin = l / a;
		int upakMax = r / a;

		int pieceMin = l % a;
		int pieceMax = r % a;

		if (pieceMin > a / 2)
		{
			pieceMin = 0;
			upakMin++;
		}
		if (pieceMax > a / 2)
		{
			pieceMax = 0;
			upakMax++;
		}

		if (upakMin * a + pieceMin > r && upakMax * a + pieceMax > r)
			cout << "YES\n";
		else
			cout << "NO\n";
	}
}


#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
   int t, l, r, a;
   double da;
   cin >> t;
   for (int i = 0; i < t; i++)
   {
      cin >> l >> r;
      a = r + 1;
      da = a;
      if (l % a >= da/2 && r % a >= da/2)
         cout << "YES" << "\n";
      else
         cout << "NO" << "\n";
   }
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int l, r;
		cin >> l >> r;
		int flag = 0;
		for (int i = l; i <= r; i++)
		{
			for (int j = 1; j <r; j++)
			{
				if (2 * (i % j) - j >= 0)
				{
					flag = 1;
				}
			}
			if (flag)
				break;
		}
		if (flag)
			cout << "YES" << endl;
		else
			cout << "NO" << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define f first
#define INTMAX 1e18+1e9
#define pb push_back
#define pencho ios_base::sync_with_stdio(false);cin.tie(NULL);
#define s second
#define int long long
#define endl '\n'

const int N = 10000005;
const int mod = 998244353;

int bp(int x, int y) {int ans = 1; x %= mod; while (y) {if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1;} return ans;}
int mod_inverse(int x) {return bp(x, mod - 2); }
int gcd (int a, int b) { while (b) {a %= b; swap(a, b);} return a;}
int max(int a, int b) {     if (a < b)    swap(a, b);   return a;}
int min(int a, int b) { if (a > b) swap(a, b); return a;}
int countSetBits(int n) {int count = 0; while (n) {count += n & 1; n >>= 1;} return count;}
/*int mul_inv(int a, int b) { int b0 = b, t, q; int x0 = 0, x1 = 1; if (b == 1) return 1; while (a > 1) { q = a / b;t = b, b = a % b, a = t;t = x0, x0 = x1 - q * x0, x1 = t;}if (x1 < 0) x1 += b0;return x1;}
int chinese_remainder(vector <int> &a, int len = 3) {vector <int> n = {26, 25, 23};int p, i, prod = 1, sum = 0;for(int i = 0; i < len; i++) prod *= n[i];for(int i = 0; i < len; i++) {p = prod / n[i];sum += a[i] * mul_inv(p, n[i]) * p;}return sum % prod;}*/

void solve(){
    pencho
    int n,m;  cin>>n>>m;
    int a[n+1], b[m+1], mn = INTMAX;
    map<int,int>last;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(mn>a[i]) mn=a[i];
        last[a[i]] = i;
    }
    int index[m+1], cur=1, flag=0;
    for(int i=1;i<=m;i++){
        cin>>b[i];
        if(last[b[i]]<cur){ flag=1; break;}
        else cur = last[b[i]];
        index[i] = cur;
    }
    if(b[1]>mn) flag=1;
    if(flag!=1){
        int second_mn = INTMAX;
        for(int i=last[b[m]];i<=n;i++)  second_mn=min(second_mn,a[i]);
        if(second_mn<last[b[m]])  flag=1;
    }
    if(flag == 1)   cout<<"0"<<endl;
    else{
        int ans = 1;
        for(int i=1;i<m;i++){
            cur=index[i+1];
            while(a[cur]>=b[i+1]) cur--;
            //cout<<index[i]<<" "<<index[i+1]<<endl;
            ans = (ans*(index[i+1]-cur))%mod;
            //cout<<ans<<endl;
        }
        cout<<ans<<endl;
    }

}

signed main()
{
#ifndef ONLINE_JUDGE
       freopen("input.txt", "r", stdin);
      // freopen("o.txt", "w", stdout);
#endif
      solve();

}


#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
#define int long long
const int mod = 998244353, N = 2e5 + 10, inf = 1e9+7;
int a[N],b[N],n,m,mn[N][20],lg[N],rng[N][2],ans = 1;
map<int,int> occ;
int find_min(int l,int r) {
    int j = lg[r-l+1];
    return min(mn[l][j],mn[r-(1<<j)+1][j]);
}
int32_t main()
{
     cin >> n >> m;
     if(m > n) return cout << 0,0;
     b[0] = -1, b[n+1]=-2;
     occ[b[0]] = 0, occ[b[m+1]] = n+1;
     rep(i,1,n) cin >> a[i],occ[a[i]]=i;
     rep(i,1,m) cin >> b[i];
     rep(i,1,n){
        if(i!=1)lg[i] = lg[i/2] + 1;
        mn[i][0] = a[i];
     }
     rep(j,1,19){
        rep(i,1,n){
            if(i + (1<<(j-1)) > n) break;
            mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
     }
     int curl = 0, curr = 0;
     rep(i,1,m){
        int pos = occ[b[i]];
        int lft=pos,rgt = pos;
        int l=max(1LL,occ[b[i-1]]+1);
        int r = pos;
        // cout << l << " " << r << "\n";
        while(l <= r){
            int mid = (l+r)/2;
            if(find_min(mid,pos) == b[i]){
                lft = mid;
                r = mid -1;
            }
            else l = mid+1;
        }
        l = pos;
        r = min(n,occ[b[i+1]]-1);
        while(l<=r){
            int mid = (l+r)/2;
            if(find_min(pos,mid) == b[i]){
                rgt = mid;
                l = mid+1;
            }
            else r = mid - 1;
        }
        // cout << lft << " " << rgt << "\n";nl;
        if(curr >= lft-1){
            curr = max(curr,rgt);
        }
        rng[i][0] = lft;
        rng[i][1] = rgt;
     }
     if(curl != 1 and curr != n) return cout <<0,0;
     rep(i,1,m-1){
        int ll = max(rng[i][0],rng[i+1][0]);
        int rr = min(rng[i][1],rng[i+1][1]);
        ans*=(rr-ll+2);
        ans%= mod;
     }
     cout << ans;

}


#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
#define int long long
const int mod = 998244353, N = 2e5 + 10, inf = 1e9+7;
int a[N],b[N],n,m,mn[N][20],lg[N],rng[N][2],ans = 1;
map<int,int> occ;
int find_min(int l,int r) {
    int j = lg[r-l+1];
    return min(mn[l][j],mn[r-(1<<j)+1][j]);
}
int32_t main()
{
     cin >> n >> m;
     if(m > n) return cout << 0,0;
     b[0] = -1, b[n+1]=-2;
     occ[b[0]] = 0, occ[b[m+1]] = n+1;
     rep(i,1,n) cin >> a[i],occ[a[i]]=i;
     rep(i,1,m) cin >> b[i];
     rep(i,1,m){
        if(occ.count(b[i]) == 0) return cout << 0,0;
     }
     rep(i,1,n){
        if(i!=1)lg[i] = lg[i/2] + 1;
        mn[i][0] = a[i];
     }
     rep(j,1,19){
        rep(i,1,n){
            if(i + (1<<(j-1)) > n) break;
            mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
     }
     int curl = 0, curr = 0;
     rep(i,1,m){
        int pos = occ[b[i]];
        int lft=pos,rgt = pos;
        int l=max(1LL,occ[b[i-1]]+1);
        int r = pos;
        // cout << l << " " << r << "\n";
        while(l <= r){
            int mid = (l+r)/2;
            if(find_min(mid,pos) == b[i]){
                lft = mid;
                r = mid -1;
            }
            else l = mid+1;
        }
        l = pos;
        r = min(n,occ[b[i+1]]-1);
        while(l<=r){
            int mid = (l+r)/2;
            if(find_min(pos,mid) == b[i]){
                rgt = mid;
                l = mid+1;
            }
            else r = mid - 1;
        }
        // cout << lft << " " << rgt << "\n";nl;
        if(curr >= lft-1){
            curr = max(curr,rgt);
        }
        rng[i][0] = lft;
        rng[i][1] = rgt;
     }
     if(curl != 1 and curr != n) return cout <<0,0;
     rep(i,1,m-1){
        int ll = max(rng[i][0],rng[i+1][0]);
        int rr = min(rng[i][1],rng[i+1][1]);
        ans*=(rr-ll+2);
        ans%= mod;
     }
     cout << ans;

}


#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
#define int long long
const int mod = 998244353, N = 2e5 + 10, inf = 1e9+7;
int a[N],b[N],n,m,mn[N][20],lg[N],rng[N][2],ans = 1;
map<int,int> occ;
int find_min(int l,int r) {
    int j = lg[r-l+1];
    return min(mn[l][j],mn[r-(1<<j)+1][j]);
}
int32_t main()
{
     cin >> n >> m;
     if(m > n) return cout << 0,0;
     b[0] = -1, b[n+1]=-2;
     occ[b[0]] = 0, occ[b[m+1]] = n+1;
     rep(i,1,n) cin >> a[i],occ[a[i]]=i;
     rep(i,1,m) cin >> b[i];
     rep(i,1,m){
        if(occ.count(b[i]) == 0) return cout << 0,0;
     }
     rep(i,1,m-1){
        if(occ[b[i]] > occ[b[i+1]]) return cout << 0,0;
     }
     rep(i,1,n){
        if(i!=1)lg[i] = lg[i/2] + 1;
        mn[i][0] = a[i];
     }
     rep(j,1,19){
        rep(i,1,n){
            if(i + (1<<(j-1)) > n) break;
            mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
     }
     int curl = 0, curr = 0;
     rep(i,1,m){
        int pos = occ[b[i]];
        int lft=pos,rgt = pos;
        int l=max(1LL,occ[b[i-1]]+1);
        int r = pos;
        // cout << l << " " << r << "\n";
        while(l <= r){
            int mid = (l+r)/2;
            if(find_min(mid,pos) == b[i]){
                lft = mid;
                r = mid -1;
            }
            else l = mid+1;
        }
        l = pos;
        r = min(n,occ[b[i+1]]-1);
        while(l<=r){
            int mid = (l+r)/2;
            if(find_min(pos,mid) == b[i]){
                rgt = mid;
                l = mid+1;
            }
            else r = mid - 1;
        }
        // cout << lft << " " << rgt << "\n";nl;
        if(curr >= lft-1){
            curr = max(curr,rgt);
        }
        rng[i][0] = lft;
        rng[i][1] = rgt;
     }
     if(curl != 1 and curr != n) return cout <<0,0;
     rep(i,1,m-1){
        int ll = max(rng[i][0],rng[i+1][0]);
        int rr = min(rng[i][1],rng[i+1][1]);

        ans*=(rr-ll+2);
        ans%= mod;
     }
     cout << ans;

}


#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
#define int long long
const int mod = 998244353, N = 2e5 + 10, inf = 1e9+7;
int a[N],b[N],n,m,mn[N][20],lg[N],rng[N][2],ans = 1;
map<int,int> occ;
int find_min(int l,int r) {
    int j = lg[r-l+1];
    return min(mn[l][j],mn[r-(1<<j)+1][j]);
}
int32_t main()
{
     cin >> n >> m;
     if(m > n) return cout << 0,0;
     b[0] = -1, b[n+1]=-2;
     occ[b[0]] = 0, occ[b[m+1]] = n+1;
     rep(i,1,n) cin >> a[i],occ[a[i]]=i;
     rep(i,1,m) cin >> b[i];
     rep(i,1,m){
        if(occ.count(b[i]) == 0) return cout << 0,0;
     }
     rep(i,1,m-1){
        if(occ[b[i]] > occ[b[i+1]]) return cout << 0,0;
     }
     rep(i,1,n){
        if(i!=1)lg[i] = lg[i/2] + 1;
        mn[i][0] = a[i];
     }
     rep(j,1,19){
        rep(i,1,n){
            if(i + (1<<(j-1)) > n) break;
            mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
     }
     int curl = 0, curr = 0;
     rep(i,1,m){
        int pos = occ[b[i]];
        int lft=pos,rgt = pos;
        int l=max(1LL,occ[b[i-1]]+1);
        int r = pos;
        // cout << l << " " << r << "\n";
        while(l <= r){
            int mid = (l+r)/2;
            if(find_min(mid,pos) == b[i]){
                lft = mid;
                r = mid -1;
            }
            else l = mid+1;
        }
        l = pos;
        r = min(n,occ[b[i+1]]-1);
        while(l<=r){
            int mid = (l+r)/2;
            if(find_min(pos,mid) == b[i]){
                rgt = mid;
                l = mid+1;
            }
            else r = mid - 1;
        }
        cout << lft << " " << rgt << "\n";nl;
        if(curr >= lft-1){
            curr = max(curr,rgt);
        }
        rng[i][0] = lft;
        rng[i][1] = rgt;
     }
     if(curl != 1 and curr != n) return cout <<0,0;
     rep(i,1,m-1){
        int ll = max(rng[i][0],rng[i+1][0]);
        int rr = min(rng[i][1],rng[i+1][1]);

        ans*=(rr-ll+2);
        ans%= mod;
     }
     cout << ans;

}


#include "bits/stdc++.h"
using namespace std;
#define dbg(var) cout<<#var<<"="<<var<<" "
#define nl cout<<"\n"
#define fr(i,n) for(int i=0;i<n;i++)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define fast ios::sync_with_stdio(false);cin.tie(0);
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define fa(v) for(auto &i:v)
#define all(v) v.begin(),v.end()
#define sz(v) (int)(v.size())
#define int long long
const int mod = 998244353, N = 2e5 + 10, inf = 1e9+7;
int a[N],b[N],n,m,mn[N][20],lg[N],rng[N][2],ans = 1;
map<int,int> occ;
int find_min(int l,int r) {
    int j = lg[r-l+1];
    return min(mn[l][j],mn[r-(1<<j)+1][j]);
}
int32_t main()
{
    fast;
     cin >> n >> m;
     b[0] = -1, b[n+1]=-2;
     occ[b[0]] = 0, occ[b[m+1]] = n+1;
     rep(i,1,n) cin >> a[i],occ[a[i]]=i;
     rep(i,1,m) cin >> b[i];
     rep(i,1,n){
        if(i!=1)lg[i] = lg[i/2] + 1;
        mn[i][0] = a[i];
     }
     rep(j,1,19){
        rep(i,1,n){
            if(i + (1<<(j-1)) > n) break;
            mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
        }
     }
     int curl = 0, curr = 0;
     rep(i,1,m){
        int pos = occ[b[i]];
        int lft=pos,rgt = pos;
        int l=max(1LL,occ[b[i-1]]+1);
        int r = pos;
        // cout << l << " " << r << "\n";
        while(l <= r){
            int mid = (l+r)/2;
            if(find_min(mid,pos) == b[i]){
                lft = mid;
                r = mid -1;
            }
            else l = mid+1;
        }
        l = pos;
        r = min(n,occ[b[i+1]]-1);
        while(l<=r){
            int mid = (l+r)/2;
            if(find_min(pos,mid) == b[i]){
                rgt = mid;
                l = mid+1;
            }
            else r = mid - 1;
        }
        // cout << lft << " " << rgt << "\n";nl;
        if(curr >= lft-1){
            curr = max(curr,rgt);
        }
        rng[i][0] = lft;
        rng[i][1] = rgt;
     }
     if(curl != 1 and curr != n) return cout <<0,0;
     rep(i,1,m-1){
        int ll = max(rng[i][0],rng[i+1][0]);
        int rr = min(rng[i][1],rng[i+1][1]);
        ans*=(rr-ll+2);
        ans%= mod;
     }
     cout << ans;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=0;
    for(i=0,j=1;i<n;i++)
    {   ll in=0;

        while(j>=0&&b[j]>a[i]){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        j++;
        if(j==m)j--;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(j!=m-1){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=0;
    for(i=0,j=1;i<n;i++)
    {   ll in=0;
        while(j>=0&&b[j]>a[i]){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        if(j<m)
        j++;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(j!=m){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=0;
    for(i=0,j=1;i<n;i++)
    {   ll in=0;
        while(j>=0&&b[j]>a[i]){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        j++;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(j!=m){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=0;
    for(i=0,j=1;i<n;i++)
    {   ll in=0;

        while(j>=0&&b[j]>a[i]){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        j++;
        if(j==m)j--;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(n<m){cout<<0;return;}
    if(j!=m-1){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    //ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=0;
    for(i=0,j=1;i<n;i++)
    {   ll in=0;

        while(j>=0&&b[j]>a[i]){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        j++;
        if(j==m)j--;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(n<m){cout<<0;return;}
    if(j!=m-1){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    //ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=-1;
    for(i=0,j=0;i<n;i++)
    {   ll in=0;

        while(j>=0&&b[j]>a[i]){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        j++;
        if(j==m)j--;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(n<m){cout<<0;return;}
    for(i=0;i<n;i++)if(e[i]==-1){cout<<0;return;}
    if(j!=m-1){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    //ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


//CODE BY DAKSH
#include<bits/stdc++.h>
#define ll          long long
#define pb          push_back
#define pii         pair<ll,ll>
#define vi          vector<ll>
#define vii         vector<pii>
#define mi          map<ll,ll>
#define mii         map<pii,ll>
#define all(a)      (a).begin(),(a).end()
#define F           first
#define S           second
#define mus         multiset<ll>
#define itr         :: iterator
#define pi          3.141592653589793238462
#define setp        fixed << setprecision(6) <<
#define mod(n)      max(n,-n)
using namespace std;
void solve()
{
    ll i=0,j,k,l,n,m,ans=0;
    cin>>n>>m;
    ll a[n],b[m],e[m],s[m];
    for(i=0;i<n;i++)cin>>a[i];
    for(i=0;i<m;i++)cin>>b[i];
    memset(e,-1,sizeof(e));
    memset(s,-1,sizeof(e));
    s[0]=0;e[0]=0;
    for(i=0,j=1;i<n;i++)
    {   ll in=0;
        while(b[j]>a[i]&&j>=0){in=1;e[j]=s[j]=-1;j--;}
        if(j==-1){cout<<0;return;}
        if(s[j]==-1)
        {
            s[j]=i;
        }
        if(b[j]==a[i])
        e[j]=i;
        j++;
//        cout<<i<<" = ";
//        for(l=0;l<m;l++)cout<<s[l]<<" ";cout<<" ---- ";
//        for(l=0;l<m;l++)cout<<e[l]<<" ";cout<<"\n";
    }
    if(j!=m){cout<<0;return;}
    for(i=1,j=1;i<m;i++)
    {
        j=(j*(e[i]-s[i]+1))%(998244353);
    }
    cout<<j;
}
signed main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    long long int t=1;
    //cin>>t;
    while(t--)
    {
        solve();
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<pair<int,int> > a;
	vector<int> v(n);
	vector<int> b(m);
	int minn=1e9;
	for(int i=0;i<n;i++){
		int x;cin>>x;
		v[i]=x;
		a.push_back({x,i});
		minn=min(x,minn);
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	if(minn<b[0]){
		cout<<0;exit(0);
	}
	map<int,int> vis;
	for(int i=0;i<m;i++){
		vis.insert({b[i],0});
	}
	for(int i=0;i<n;i++){
		if(vis.count(a[i].first)){
			vis[a[i].first]++;
		}
	}
	for(auto it:vis){
		if(it.second==0){
			cout<<0;exit(0);
		}
	}

	sort(a.begin(),a.end());
	int i=1,j=0;int id=-1;
	vector<int> c;
	int x=0;
	while(j<n&&i<m){
		x=0;
		if(a[j].first<b[i]){
			id=max(id,a[j].second);j++;
		}
		else{
			x=1;
			c.push_back(id+1);id=a[j].second;i++;j++;
		}
	}
	if(x==0)c.push_back(id+1);
	if(c.back()!=n)c.push_back(n);
	for(int i=1;i<(int)c.size();i++){
		if(c[i-1]>=c[i]){
			cout<<0;exit(0);
		}
	}
	ll ans=1;
	for(int i=1;i<(int)c.size();i++){
		ll y=0;
		for(int j=c[i-1];j<c[i];j++){
			// cout<<vis[v[j]]<<endl;
			if(vis[b[i]]==0){

				break;
			}
			if(vis.count(v[j])){
				// cout<<"HH "<<endl;
				vis[v[j]]--;
			}
			y++;

		}
		ans*=y;
		ans%=998244353;
	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<pair<int,int> > a;
	vector<int> v(n);
	vector<int> b(m);
	for(int i=0;i<n;i++){
		int x;cin>>x;
		a.push_back({x,i+1});
		v[i]=x;
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	sort(a.begin(),a.end());
	int j=0,i=1;
	int id=-1;
	vector<int> c;
	while(j<n&&i<m){
		if(b[i]>a[j].first){
			id=max(id,a[j].second);j++;
		}
		else{
			c.push_back(id);id=a[j].second;j++;;i++;
		}
	}
	c.push_back(n);
	ll ans=1;ll r=998244353;
	for(int i=0;i<(int)c.size();i++){
		int x=1e9+1;int y=0;
		int start;
		if(i>0)start=c[i-1];
		else start=0;
		for(int j=start;j<c[i];j++){
			x=min(x,v[j]);
			if(b[i]==v[j])y++;
		}
		if(x!=b[i]){
			cout<<0;exit(0);
		}
		else if(i>0){
			ans*=y;ans%=r;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<int> a(n);
	vector<int>b(m);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	vector<int> data(m);
	vector<bool> check(m);
	for(int i=0;i<n;i++){
		auto low=lower_bound(b.begin(),b.end(),a[i]);
		int id=low-b.begin();
		if(b[id]==a[i])check[id]=true;
		if(a[i]!=b[id]){
			if(id>0)data[id-1]=i+1;
		}
		else{
			data[id]=i+1;
		}

	}
	for(int i=0;i<m;i++){
		cout<<data[i]<<" ";
	}
	ll r=998244353;
	int start=0;ll ans=1;
	for(int i=0;i<m;i++){
		int y=0;int x=1e9+1;
		for(int j=start;j<data[i];j++){
			if(a[j]==b[i])y++;
			x=min(x,a[j]);
		}
		start=data[i];
		if(x!=b[i]||y==0){
			cout<<0;exit(0);
		}
		else{
			ans*=y;ans%=r;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<pair<int,int> > a;
	vector<int> v(n);
	vector<int> b(m);
	int minn=1e9;
	for(int i=0;i<n;i++){
		int x;cin>>x;
		v[i]=x;
		a.push_back({x,i});
		minn=min(x,minn);
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	if(minn<b[0]){
		cout<<0;exit(0);
	}
	map<int,int> vis;
	for(int i=0;i<m;i++){
		vis.insert({b[i],0});
	}
	for(int i=0;i<n;i++){
		if(vis.count(a[i].first)){
			vis[a[i].first]++;
		}
	}
	for(auto it:vis){
		if(it.second==0){
			cout<<0;exit(0);
		}
	}
	if(m==1){
		cout<<1;exit(0);
	}
	sort(a.begin(),a.end());
	int i=1,j=0;int id=-1;
	vector<int> c;
	int x=0;
	while(j<n&&i<m){
		x=0;
		if(a[j].first<b[i]){
			id=max(id,a[j].second);j++;
		}
		else{
			x=1;
			c.push_back(id+1);id=a[j].second;i++;j++;
		}
	}
	if(x==0)c.push_back(id+1);
	if(c.back()!=n)c.push_back(n);
	for(int i=1;i<(int)c.size();i++){
		if(c[i-1]>=c[i]){
			cout<<0;exit(0);
		}
	}
	ll ans=1;
	for(int i=1;i<(int)c.size();i++){
		ll y=0;
		for(int j=c[i-1];j<c[i];j++){
			// cout<<vis[v[j]]<<endl;
			if(vis[b[i]]==0){

				break;
			}
			if(vis.count(v[j])){
				// cout<<"HH "<<endl;
				vis[v[j]]--;
			}
			y++;

		}
		ans*=y;
		ans%=998244353;
	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<pair<int,int> > a;
	vector<int> b(m);
	for(int i=0;i<n;i++){
		int x;cin>>x;
		a.push_back({x,i+1});
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	sort(a.begin(),a.end());
	int j=0,i=1;
	int id=-1;
	vector<int> c;
	while(j<n&&i<m){
		if(b[i]>a[j].first){
			id=max(id,a[j].second);j++;
		}
		else{
			c.push_back(id);id=-1;i++;
		}
	}
	c.push_back(n);
	ll ans=1;ll r=998244353;
	for(int i=0;i<(int)c.size();i++){
		int x=1e9+1;int y=0;
		int start;
		if(i>0)start=c[i-1];
		else start=0;
		for(int j=start;j<c[i];j++){
			x=min(x,a[j].first);
			if(b[i]==a[j].first)y++;
		}
		if(x!=b[i]){
			cout<<0;exit(0);
		}
		else if(i>0){
			ans*=y;ans%=r;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<pair<int,int> > a;
	vector<int> v(n);
	vector<int> b(m);
	int minn=1e9;
	for(int i=0;i<n;i++){
		int x;cin>>x;
		v[i]=x;
		a.push_back({x,i});
		minn=min(x,minn);
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	if(minn<b[0]){
		cout<<0;exit(0);
	}
	map<int,int> vis;
	for(int i=0;i<m;i++){
		vis.insert({b[i],0});
	}
	for(int i=0;i<n;i++){
		if(vis.count(a[i].first)){
			vis[a[i].first]++;
		}
	}
	for(auto it:vis){
		if(it.second==0){
			cout<<0;exit(0);
		}
	}
	if(m==1){
		cout<<1;exit(0);
	}
	sort(a.begin(),a.end());
	int i=1,j=0;int id=-1;
	vector<int> c;
	int x=0;
	while(j<n&&i<m){
		x=0;
		if(a[j].first<b[i]){
			id=max(id,a[j].second);j++;
		}
		else{
			x=1;
			c.push_back(id+1);id=a[j].second;i++;j++;
		}
	}
	if(x==0)c.push_back(id+1);
	if(c.back()!=n)c.push_back(n);
	for(int i=1;i<(int)c.size();i++){
		if(c[i-1]>=c[i]){
			cout<<0;exit(0);
		}
	}
	if((int)c.size()!=m){
		cout<<0;exit(0);
	}
	// for(int i=0;i<(int)c.size();i++){
	// 	cout<<c[i]<<" ";
	// }
	// cout<<endl;
	ll ans=1;
	for(int i=0;i<c[0];i++){
		if(vis.count(v[i]))vis[v[i]]--;
	}
	for(int i=1;i<(int)c.size();i++){
		ll y=0;
		for(int j=c[i-1];j<c[i];j++){
			if(vis[b[i]]==0){

				break;
			}
			if(vis.count(v[j])){
				vis[v[j]]--;
			}
			y++;

		}
		ans*=y;
		ans%=998244353;
	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<int> a(n);
	vector<int>b(m);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	vector<int> data(m);
	vector<bool> check(m);
	for(int i=0;i<n;i++){
		auto low=lower_bound(b.begin(),b.end(),a[i]);
		int id=low-b.begin();
		// if(b[id]==a[i])check[id]=true;
		id=min(id,m-1);
		// id=max(0,id);
		if(a[i]!=b[id]){
			if(id>0)data[id-1]=i+1;
		}
		else{
			data[id]=i+1;
		}

	}
	// for(int i=0;i<m;i++){
	// 	cout<<data[i]<<" ";
	// }
	ll r=998244353;
	int start=0;ll ans=1;
	for(int i=0;i<m;i++){
		int y=0;int x=1e9+1;
		for(int j=start;j<data[i];j++){
			if(a[j]==b[i])y++;
			x=min(x,a[j]);
		}
		start=data[i];
		if(x!=b[i]||y==0){
			cout<<0;exit(0);
		}
		else{
			ans*=y;ans%=r;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<pair<int,int> > a;
	vector<int> v(n);
	vector<int> b(m);
	int minn=1e9;
	for(int i=0;i<n;i++){
		int x;cin>>x;
		v[i]=x;
		a.push_back({x,i});
		minn=min(x,minn);
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	if(minn<b[0]){
		cout<<0;exit(0);
	}
	map<int,int> vis;
	for(int i=0;i<m;i++){
		vis.insert({b[i],0});
	}
	for(int i=0;i<n;i++){
		if(vis.count(a[i].first)){
			vis[a[i].first]++;
		}
	}
	for(auto it:vis){
		if(it.second==0){
			cout<<0;exit(0);
		}
	}
	if(m==1){
		cout<<1;exit(0);
	}
	sort(a.begin(),a.end());
	int i=1,j=0;int id=-1;
	vector<int> c;
	int x=0;
	while(j<n&&i<m){
		x=0;
		if(a[j].first<b[i]){
			id=max(id,a[j].second);j++;
		}
		else{
			x=1;
			c.push_back(id+1);id=a[j].second;i++;j++;
		}
	}
	if(x==0)c.push_back(id+1);
	if(c.back()!=n)c.push_back(n);
	for(int i=1;i<(int)c.size();i++){
		if(c[i-1]>=c[i]){
			cout<<0;exit(0);
		}
	}
	if((int)c.size()!=m){
		cout<<0;exit(0);
	}
	ll ans=1;
	for(int i=1;i<(int)c.size();i++){
		ll y=0;
		for(int j=c[i-1];j<c[i];j++){
			if(vis[b[i]]==0){

				break;
			}
			if(vis.count(v[j])){
				vis[v[j]]--;
			}
			y++;

		}
		ans*=y;
		ans%=998244353;
	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<int> a(n);
	vector<int>b(m);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	vector<int> data(m);
	vector<bool> check(m);
	for(int i=0;i<n;i++){
		auto low=lower_bound(b.begin(),b.end(),a[i]);
		int id=low-b.begin();
		if(b[id]==a[i])check[id]=true;
		if(a[i]!=b[id]){
			if(id>0)data[id-1]=i+1;
		}
		else{
			data[id]=i+1;
		}

	}
	// for(int i=0;i<m;i++){
	// 	cout<<data[i]<<" ";
	// }
	ll r=998244353;
	int start=0;ll ans=1;
	for(int i=0;i<m;i++){
		int y=0;int x=1e9+1;
		for(int j=start;j<data[i];j++){
			if(a[j]==b[i])y++;
			x=min(x,a[j]);
		}
		start=data[i];
		if(x!=b[i]||y==0){
			cout<<0;exit(0);
		}
		else{
			ans*=y;ans%=r;
		}

	}
	cout<<ans;

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	int n,m;cin>>n>>m;
	vector<int> a(n);
	vector<int>b(m);
	for(int i=0;i<n;i++){
		cin>>a[i];
	}
	for(int i=0;i<m;i++){
		cin>>b[i];
	}
	vector<int> data(m);
// 	vector<bool> check(m);
	for(int i=0;i<n;i++){
		auto low=lower_bound(b.begin(),b.end(),a[i]);
		int id=low-b.begin();
		// if(b[id]==a[i])check[id]=true;
		if(a[i]!=b[id]){
			if(id>0)data[id-1]=i+1;
		}
		else{
			data[id]=i+1;
		}

	}
	// for(int i=0;i<m;i++){
	// 	cout<<data[i]<<" ";
	// }
	ll r=998244353;
	int start=0;ll ans=1;
	for(int i=0;i<m;i++){
		int y=0;int x=1e9+1;
		for(int j=start;j<data[i];j++){
			if(a[j]==b[i])y++;
			x=min(x,a[j]);
		}
		start=data[i];
		if(x!=b[i]||y==0){
			cout<<0;exit(0);
		}
		else{
			ans*=y;ans%=r;
		}

	}
	cout<<ans;

}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
        int lft=f[b[i]];
        int rgt=l[b[i]];
        if(i==1) lft=1;
        if(i==m) rgt=n;
        rep(j,lft,rgt) if(a[j]<b[i]) return cout<<0,0;
    }
    ll ans=1;
    rep(i,2,m) ans=ans*(l[b[i]]-f[b[i]]+1)%mod;
    cout<<ans;
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
        f[b[i]]=max(f[b[i]],l[b[i-1]]+1);
        int lft=f[b[i]];
        int rgt=l[b[i]];
        if(i==1) lft=1;
        if(i==m) rgt=n;
        rep(j,lft,rgt) if(a[j]<b[i]) return cout<<0,0;
    }
    ll ans=1;
    rep(i,2,m) ans=ans*(l[b[i]]-f[b[i]]+1)%mod;
    cout<<ans;
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N],pos[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
    }
    stack<int>s;
    s.push(0);
    rep(i,1,n){
        while(a[s.top()]>=a[i]) s.pop();
        pos[i]=s.top();
        s.push(i);
    }
    ll ans=1,lft=1,rgt;
    rep(i,1,m){
        rgt=pos[l[b[i+1]]];
        if(i==m) rgt=n;
        int last=-1;
        rep(j,lft,rgt){
            if(a[j]<b[i]) return cout<<0,0;
            if(a[j]==b[i]) last=j;
        }
        if(last==-1) return cout<<-1,0;
        if(i>1) ans=ans*(last-lft+1)%mod;
        lft=rgt+1;
//        cout<<f[b[i]]<<"--"<<l[b[i]]<<endl;
    }
    cout<<ans;
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N],pos[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
    }
    stack<int>s;
    s.push(0);
    rep(i,1,n){
        while(a[s.top()]>=a[i]) s.pop();
        pos[i]=s.top();
        s.push(i);
    }
    ll ans=1,lft=1,rgt;
    rep(i,1,m){
        rgt=pos[l[b[i+1]]];
        if(i==m) rgt=n;
        int last=-1;
        rep(j,lft,rgt){
            if(a[j]<b[i]) return cout<<0,0;
            if(a[j]==b[i]) last=j;
        }
        if(last==-1) return cout<<0,0;
        if(i>1) ans=ans*(last-lft+1)%mod;
        lft=rgt+1;
//        cout<<f[b[i]]<<"--"<<l[b[i]]<<endl;
    }
    cout<<ans;
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N],pos[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
    }
    stack<int>s;
    s.push(0);
    rep(i,1,n){
        while(a[s.top()]>=a[i]) s.pop();
        pos[i]=s.top();
        s.push(i);
    }
    ll ans=1,lft=1,rgt;
    rep(i,1,m){
        rgt=pos[l[b[i+1]]];
        if(i==m) rgt=n;
        int last=-1;
        rep(j,lft,rgt){
            if(a[j]<b[i]) return cout<<0,0;
            if(a[j]==b[i]) last=j;
        }
        if(lft==-1) return cout<<-1,0;
        if(i>1) ans=ans*(last-lft+1)%mod;
        lft=rgt+1;
//        cout<<f[b[i]]<<"--"<<l[b[i]]<<endl;
    }
    cout<<ans;
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N],pos[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
    }
    stack<int>s;
    s.push(0);
    rep(i,1,n){
        while(a[s.top()]>=a[i]) s.pop();
        pos[i]=s.top();
        s.push(i);
    }
    ll ans=1;
    rep(i,1,m){
        f[b[i]]=pos[l[b[i]]]+1;
        l[b[i]]=pos[l[b[i+1]]];
        int &lft=f[b[i]];
        int &rgt=l[b[i]];
        if(i==1) lft=1;
        if(i==m) rgt=n;
        int last=-1;
        rep(j,lft,rgt){
            if(a[j]<b[i]) return cout<<0,0;
            if(a[j]==b[i]) last=j;
        }
        if(i>1) ans=ans*(last-lft+1)%mod;
//        cout<<f[b[i]]<<"--"<<l[b[i]]<<endl;
    }
    cout<<ans;
    return 0;
}


/**
 *    Coded by : lucky_21
 *               --------Lokesh Singh
**/

#include<bits/stdc++.h>
using namespace std;

#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T> using oset=tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

#define     F           first
#define     S           second
#define     pb          push_back
#define     lb          lower_bound
#define     ub          upper_bound
#define     pii         pair<int,int>
#define     all(x)      x.begin(),x.end()
#define     fix         fixed<<setprecision(10)
#define     rep(i,a,b)  for(int i=int(a);i<=int(b);i++)
#define     repb(i,b,a) for(int i=int(b);i>=int(a);i--)
#define     FastIO      ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

typedef double db;
typedef long long ll;

const int N=2e5+5;
const int mod=998244353;

int n,m,a[N],b[N],pos[N];
signed main(){
    FastIO;
    cin>>n>>m;
    map<int,int>f,l;
    rep(i,1,n){
        cin>>a[i];
        if(!f.count(a[i])) f[a[i]]=i;
        l[a[i]]=i;
    }
    rep(i,1,m){
        cin>>b[i];
        if(!f.count(b[i])) return cout<<0,0;
    }
    stack<int>s;
    s.push(0);
    rep(i,1,n){
        while(a[s.top()]>=a[i]) s.pop();
        pos[i]=s.top();
        s.push(i);
    }
    ll ans=1,lft=1,rgt;
    rep(i,1,m){
        rgt=pos[l[b[i+1]]];
        if(i==m) rgt=n;
        int last=-1;
        rep(j,lft,rgt){
            if(a[j]<b[i]) return cout<<0,0;
            if(a[j]==b[i]) last=j;
        }
        if(i>1) ans=ans*(last-lft+1)%mod;
        lft=rgt+1;
//        cout<<f[b[i]]<<"--"<<l[b[i]]<<endl;
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=2e5+10;
const int inf=0x3f3f3f3f;
const ll mod=998244353;
const int bit=32;
int a[M],b[M];
map<int,int>mm;
struct node{
	int l,r;
}w[M];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		mm[a[i]]=i;
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
	}
	int f=1;
	a[0]=0;
	for(int i=1;i<=m;i++){
		int p=mm[b[i]];
		int r;
		if(i!=m)
		r=mm[b[i+1]];
		else
		r=n+1;
		if(p){
			for(int j=p;j<=r;j++){
				if(b[i]<=a[j]&&j!=r){
					continue;
				}
				else{
					w[i].r=j;
					break;
				}
			}
			for(int j=p;j>=0;j--){
				if(b[i]<=a[j])
				continue;
				else{
					w[i].l=j;
					break;
				}
			}
		}
		else
		f=0;
	}
	if(w[1].l!=0||w[m].r!=n+1)
	f=0;
	if(!f){
		puts("0");
	}
	else{
		ll ans=1;
		for(int i=2;i<=m;i++){
			int l=w[i].l;
			int r=w[i-1].r;
			if(l>r)
			f=0;
			else{
				ans=ans*(r-l)%mod;
			}
		}
		if(!f)
		puts("0");
		else
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=2e5+10;
const int inf=0x3f3f3f3f;
const ll mod=998244353;
const int bit=32;
int a[M],b[M];
map<int,int>mm;
struct node{
	int l,r;
}w[M];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		mm[a[i]]=i;
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
	}
	int f=1;
	a[0]=0;
	for(int i=1;i<=m;i++){
		int p=mm[b[i]];
		int r,l;
		if(i!=m)
		r=mm[b[i+1]];
		else
		r=n+1;
		if(l!=1)
		l=mm[b[i-1]];
		else
		l=0;
		if(p){
			for(int j=p;j<=r;j++){
				if(b[i]<=a[j]&&j!=r){
					continue;
				}
				else{
					w[i].r=j;
					break;
				}
			}
			for(int j=p;j>=l;j--){
				if(b[i]<=a[j]&&j!=l)
				continue;
				else{
					w[i].l=j;
					break;
				}
			}
		}
		else
		f=0;
	}
	if(w[1].l!=0||w[m].r!=n+1)
	f=0;
	if(!f){
		puts("0");
	}
	else{
		ll ans=1;
		for(int i=2;i<=m;i++){
			int l=w[i].l;
			int r=w[i-1].r;
			if(l>r)
			f=0;
			else{
				ans=ans*(r-l)%mod;
			}
		}
		if(!f)
		puts("0");
		else
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=5e5+10;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int bit=32;
int a[500010],ans[M][2];
int gcd(int a,int b) {
	return b==0?a:gcd(b,a%b);
}
int main(){
	int n;
	scanf("%d",&n);
	int sum=0;
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		ans[i][0]=-1;
		ans[i][1]=-1;
		int p=x;
		for(int j=2;j*j<=p;j++){
			if(p%j==0){
				if(gcd(j+x/j,x)==1){
					ans[i][0]=j;
					ans[i][1]=x/j;
					break;
				}
				while(p%j==0)
				p/=j;
			}
		}
		if(p>1){
			if(gcd(p+x/p,x)==1){
				ans[i][0]=p;
				ans[i][1]=x/p;
			}
		}
	}
	for(int i=1;i<=n;i++){
		printf(i==n?"%d\n":"%d ",ans[i][0]);
	}
	for(int i=1;i<=n;i++){
		printf(i==n?"%d\n":"%d ",ans[i][1]);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=2e5+10;
const int inf=0x3f3f3f3f;
const ll mod=998244353;
const int bit=32;
int a[M],b[M];
map<int,int>mm;
struct node{
	int l,r;
}w[M];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		mm[a[i]]=i;
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
	}
	int f=1;
	a[0]=0;
	for(int i=1;i<=m;i++){
		int p=mm[b[i]];
		int r;
		if(i!=m)
		r=mm[b[i+1]];
		else
		r=n+1;
		if(p){
			for(int j=p;j<=r;j++){
				if(b[i]<=a[j]&&j!=r){
					continue;
				}
				else{
					w[i].r=j;
					break;
				}
			}
			for(int j=p;j>=0;j--){
				if(b[i]<=a[j])
				continue;
				else{
					w[i].l=j;
					break;
				}
			}
		}
		else
		f=0;
	}
	if(w[1].l!=0)
	f=0;
	if(!f){
		puts("0");
	}
	else{
		ll ans=1;
		for(int i=2;i<=m;i++){
			int l=w[i].l;
			int r=w[i-1].r;
			if(l>r)
			f=0;
			else{
				ans=ans*(r-l)%mod;
			}
		}
		if(!f)
		puts("0");
		else
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=2e5+10;
const int inf=0x3f3f3f3f;
const ll mod=998244353;
const int bit=32;
int a[M],b[M];
map<int,int>mm;
struct node{
	int l,r;
}w[M];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		mm[a[i]]=i;
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
	}
	int f=1;
	a[0]=0;
	for(int i=1;i<=m;i++){
		int p=mm[b[i]];
		if(p){
			for(int j=p;j<=n+1;j++){
				if(b[i]<=a[j]){
					continue;
				}
				else{
					w[i].r=j;
					break;
				}
			}
			for(int j=p;j>=0;j--){
				if(b[i]<=a[j])
				continue;
				else{
					w[i].l=j;
					break;
				}
			}
		}
		else
		f=0;
	}
	if(w[1].l!=0)
	f=0;
	if(!f){
		puts("0");
	}
	else{
		ll ans=1;
		for(int i=2;i<=m;i++){
			int l=w[i].l;
			int r=min(w[i-1].r,mm[b[i]]);
			if(l>r)
			f=0;
			else{
				ans=ans*(r-l)%mod;
			}
		}
		if(!f)
		puts("0");
		else
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=2e5+10;
const int inf=0x3f3f3f3f;
const ll mod=998244353;
const int bit=32;
int a[M],b[M],l[M];
map<int,int>mm;
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
	}
	l[n]=a[n];
	mm[a[n]]++;
	for(int i=n-1;i>=1;i--){
		l[i]=min(l[i+1],a[i]);
		mm[l[i]]++;
	}
	if(l[1]!=b[1]){
		puts("0");
		return 0;
	}
	ll ans=1;
	for(int i=2;i<=m;i++){
		ans=ans*mm[b[i]]%mod;
	}
	printf("%lld\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=1e7+10;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const int bit=32;
int v[M],p[M],cnt=0;
void inti(){
	for(int i=2;i<M;i++){
		if(!v[i]){
			p[cnt++]=i;
		}
		for(int j=0;j<cnt&&p[j]*i<M;j++){
			v[p[j]*i]=p[j];
			if(i%p[j]==0)
			break;
		}
	}
}
int a[M],b[M];
int main(){
	int n;
	inti();
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		int x;
		scanf("%d",&x);
		int p=v[x];
		if(p==0){
			a[i]=-1;
			b[i]=-1;
			continue;
		}
		while(x%p==0)
		x/=p;
		if(x!=1){
			a[i]=x;
			b[i]=p;
		}
		else
		a[i]=-1,b[i]=-1;
	}
	for(int i=1;i<=n;i++)
	printf(i==n?"%d\n":"%d ",a[i]);
	for(int i=1;i<=n;i++)
	printf(i==n?"%d\n":"%d ",b[i]);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mem(x,y) memset(x,y,sizeof(x))
const int M=2e5+10;
const int inf=0x3f3f3f3f;
const ll mod=998244353;
const int bit=32;
int a[M],b[M];
map<int,int>mm;
struct node{
	int l,r;
}w[M];
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		mm[a[i]]=i;
	}
	for(int i=1;i<=m;i++){
		scanf("%d",&b[i]);
	}
	int f=1;
	a[0]=0;
	for(int i=1;i<=m;i++){
		int p=mm[b[i]];
		int r;
		if(i!=m)
		r=mm[b[i+1]];
		else
		r=n+1;
		if(p){
			for(int j=p;j<=r;j++){
				if(b[i]<=a[j]&&j!=r){
					continue;
				}
				else{
					w[i].r=j;
					break;
				}
			}
			for(int j=p;j>=0;j--){
				if(b[i]<=a[j])
				continue;
				else{
					w[i].l=j;
					break;
				}
			}
		}
		else
		f=0;
	}
	if(w[1].l!=0||w[n].r!=n+1)
	f=0;
	if(!f){
		puts("0");
	}
	else{
		ll ans=1;
		for(int i=2;i<=m;i++){
			int l=w[i].l;
			int r=w[i-1].r;
			if(l>r)
			f=0;
			else{
				ans=ans*(r-l)%mod;
			}
		}
		if(!f)
		puts("0");
		else
		printf("%lld\n",ans);
	}
	return 0;
}


//
// Created by fssqawj on 2020/6/17.
//

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int value;
    int idx;

public:
    Node(int value, int idx) {
        this->value = value;
        this->idx = idx;
    }

    friend bool operator < (Node a, Node b)
    {
        if (a.value == b.value) {
            return a.idx < b.idx;
        }
        return a.value > b.value;
    }
};

const int maxn = 200005;

int a[maxn], b[maxn];

priority_queue<Node> q;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0;i < n;i ++) {
        scanf("%d", &a[i]);
        q.push(Node(a[i], i));
    }
    for (int i = 0;i < m;i ++) {
        scanf("%d", &b[i]);
    }

    long long res = 1;
    bool flag = true;
    int preIdx = -1;
    for (int i = 0;i < m;i ++) {
        if (q.empty()) {
            flag = false;
            break;
        }
        Node node = q.top();
        q.pop();
        if (node.idx < preIdx || (node.value < b[i] && i > 0)) {
            i --;
            continue;
        }
        if (node.value != b[i]) {
            flag = false;
            break;
        }
        if (i > 0) {
            int tid = node.idx;
            while(tid > preIdx && a[tid] >= b[i]) {
                tid --;
            }
            res = res * (node.idx - tid) % 998244353;
        }
        preIdx = node.idx;
        while(!q.empty() && q.top().value == b[i]) {
            q.pop();
        }
    }
    if (!flag) {
        res = 0;
    }
    printf("%lld\n", res);
    return 0;
}


//
// Created by fssqawj on 2020/6/17.
//

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int value;
    int idx;

public:
    Node(int value, int idx) {
        this->value = value;
        this->idx = idx;
    }

    friend bool operator < (Node a, Node b)
    {
        if (a.value == b.value) {
            return a.idx < b.idx;
        }
        return a.value > b.value;
    }
};

const int maxn = 200005;

int a[maxn], b[maxn];

priority_queue<Node> q;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0;i < n;i ++) {
        scanf("%d", &a[i]);
        q.push(Node(a[i], i));
    }
    for (int i = 0;i < m;i ++) {
        scanf("%d", &b[i]);
    }

    long long res = 1;
    int idx = 0;
    int cr = -1;
    int pi = -1;
    bool flag = true;
    while(!q.empty()) {
        Node node = q.top();
        q.pop();
        if (node.value == b[idx]) {
            if (cr > node.idx) {
                flag = false;
                break;
            }
            int tid = node.idx;
            if (idx > 0) {
                while (tid > pi && a[tid] >= b[idx]) tid--;
                res = res * (node.idx - tid) % 998244353;
            }
            while(!q.empty() && q.top().value == b[idx]) q.pop();
            pi = node.idx;
            idx += 1;
        } else if (node.value < b[idx]) {
            if (idx == 0) {
                flag = false;
                break;
            }
            cr = max(cr, node.idx);
        } else {
            flag = false;
            break;
        }
    }
    if (!flag) {
        res = 0;
    }
    printf("%lld\n", res);
    return 0;
}


//
// Created by fssqawj on 2020/6/17.
//

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int value;
    int idx;

public:
    Node(int value, int idx) {
        this->value = value;
        this->idx = idx;
    }

    friend bool operator < (Node a, Node b)
    {
        if (a.value == b.value) {
            return a.idx < b.idx;
        }
        return a.value > b.value;
    }
};

const int maxn = 100005;

int a[maxn], b[maxn];

priority_queue<Node> q;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0;i < n;i ++) {
        scanf("%d", &a[i]);
        q.push(Node(a[i], i));
    }
    for (int i = 0;i < m;i ++) {
        scanf("%d", &b[i]);
    }

    long long res = 1;
    bool flag = true;
    int preIdx = -1;
    for (int i = 0;i < m;i ++) {
        if (q.empty()) {
            flag = false;
            break;
        }
        Node node = q.top();
        q.pop();
        if (node.idx < preIdx || (node.value < b[i] && i > 0)) {
            i --;
            continue;
        }
        if (node.value != b[i]) {
            flag = false;
            break;
        }
        if (i > 0) {
            int tid = node.idx;
            while(tid > preIdx && a[tid] >= b[i]) {
                tid --;
            }
            res = res * (node.idx - tid) % 998244353;
        }
        preIdx = node.idx;
        while(!q.empty() && q.top().value == b[i]) {
            q.pop();
        }
    }
    if (!flag) {
        res = 0;
    }
    printf("%lld\n", res);
    return 0;
}


//
// Created by fssqawj on 2020/6/17.
//

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int value;
    int idx;

public:
    Node(int value, int idx) {
        this->value = value;
        this->idx = idx;
    }

    friend bool operator < (Node a, Node b)
    {
        if (a.value == b.value) {
            return a.idx < b.idx;
        }
        return a.value > b.value;
    }
};

const int maxn = 200005;

int a[maxn], b[maxn];

priority_queue<Node> q;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0;i < n;i ++) {
        scanf("%d", &a[i]);
        q.push(Node(a[i], i));
    }
    for (int i = 0;i < m;i ++) {
        scanf("%d", &b[i]);
    }

    long long res = 1;
    int idx = 0;
    int cr = -1;
    int pi = -1;
    bool flag = true;
    while(!q.empty() && idx < m) {
        Node node = q.top();
        q.pop();
        if (node.value == b[idx]) {
            if (cr > node.idx) {
                flag = false;
                break;
            }
            int tid = node.idx;
            if (idx > 0) {
                while (tid > pi && a[tid] >= b[idx]) tid--;
                res = res * (node.idx - tid) % 998244353;
            }
            while(!q.empty() && q.top().value == b[idx]) q.pop();
            pi = node.idx;
            idx += 1;
        } else if (node.value < b[idx]) {
            if (idx == 0) {
                flag = false;
                break;
            }
            cr = max(cr, node.idx);
        } else {
            flag = false;
            break;
        }
    }
    if (!flag) {
        res = 0;
    }
    printf("%lld\n", res);
    return 0;
}


//
// Created by fssqawj on 2020/6/17.
//

#include <bits/stdc++.h>

using namespace std;

struct Node {
    int value;
    int idx;

public:
    Node(int value, int idx) {
        this->value = value;
        this->idx = idx;
    }

    friend bool operator < (Node a, Node b)
    {
        if (a.value == b.value) {
            return a.idx < b.idx;
        }
        return a.value > b.value;
    }
};

const int maxn = 200005;

int a[maxn], b[maxn];

priority_queue<Node> q;

int main() {
    int n, m;
    scanf("%d %d", &n, &m);
    for (int i = 0;i < n;i ++) {
        scanf("%d", &a[i]);
        q.push(Node(a[i], i));
    }
    for (int i = 0;i < m;i ++) {
        scanf("%d", &b[i]);
    }

    long long res = 1;
    int idx = 0;
    int cr = -1;
    int pi = -1;
    bool flag = true;
    while(!q.empty() && idx < m) {
        Node node = q.top();
        q.pop();
        if (node.value == b[idx]) {
            if (cr > node.idx) {
                flag = false;
                break;
            }
            int tid = node.idx;
            if (idx > 0) {
                while (tid > pi && a[tid] >= b[idx]) tid--;
                res = res * (node.idx - tid) % 998244353;
            }
            while(!q.empty() && q.top().value == b[idx]) q.pop();
            pi = node.idx;
            idx += 1;
        } else if (node.value < b[idx]) {
            if (idx == 0) {
                flag = false;
                break;
            }
            cr = max(cr, node.idx);
        } else {
            flag = false;
            break;
        }
    }
    if (!flag || idx < m) {
        res = 0;
    }
    printf("%lld\n", res);
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli  ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 998244353;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast;
    lli n,m;cin>>n>>m;
    map<lli,lli> maxpos;
    vector<plli> a(n);
    flli(i,0,n-1)
    {
    	cin>>a[i].first;
    	a[i].second=i;
    	maxpos[a[i].first]=i+1;
    }
    sort(all(a));
    vlli b(m);
    bool flag=true;
    flli(i,0,m-1)
    {
    	cin>>b[i];
    	if(maxpos[b[i]]==0)
    	{
    		flag=false;
    	}
    }
    if(b[0]!=a[0].first)flag=false;
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    lli curr=0;
    lli start=0;
    lli minend=start;
    lli maxend=start;
    lli ans=1;
    flli(i,0,m-1)
    {
    	if(start>=n){flag=false;break;}
    	if(i==m-1)
    	{
    		continue;//I don't have any choice since all left will be the last subarray
    	}
    	lli next=b[i+1];
    	minend=start;
    	while(curr<n && a[curr].first<next)
    	{
    		minend=max(minend,a[curr].second);
    		curr++;
    	}
    	//from atleast start to end would be in one subarray
    	lli mini=INF;
    	while(curr<n && a[curr].first==next)
    	{
   			if(a[curr].second>minend)
   			{
   				mini=min(mini,a[curr].second);
   			}
   			curr++;
    	}
    	if(mini==INF)
    	{
    		flag=false;
    		break;
    	}
    	maxend=maxpos[next]-2;
    	//cout<<b[i]<<" "<<maxend<<" "<<minend<<endl;
    	ans=((maxend-minend+1)*(ans))%mod;
    	start=max(start,mini);
    }
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    cout<<ans;
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli  ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 998244353;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast;
    lli n,m;cin>>n>>m;
    map<lli,lli> maxpos;
    vector<plli> a(n);
    flli(i,0,n-1)
    {
    	cin>>a[i].first;
    	a[i].second=i;
    	maxpos[a[i].first]=i+1;
    }
    sort(all(a));
    vlli b(m);
    bool flag=true;
    flli(i,0,m-1)
    {
    	cin>>b[i];
    	if(maxpos[b[i]]==0)
    	{
    		flag=false;
    	}
    }
    if(b[0]!=a[0].first)flag=false;
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    lli curr=0;
    lli start=0;
    lli ans=1;
    flli(i,0,m-1)
    {
    	if(i==m-1)
    	{
    		if(start>=n)flag=false;
    		continue;//I don't have any choice since all left will be the last subarray
    	}
    	lli next=b[i+1];
    	lli minend=start;
    	while(curr<n && a[curr].first<next)
    	{
    		minend=max(minend,a[curr].second);
    		curr++;
    	}
    	//from atleast start to end would be in one subarray
    	lli maxend=minend;
    	lli mini=INF;
    	while(curr<n && a[curr].first==next)
    	{
   			if(a[curr].second>minend)
   			{
   				mini=min(mini,a[curr].second);
   				if(curr+1<n && a[curr+1].first==next)
   					maxend=max(maxend,a[curr].second);
   			}
   			curr++;
    	}
    	if(mini==INF)
    	{
    		flag=false;
    		break;
    	}
    	//cout<<b[i]<<" "<<maxend<<" "<<minend<<endl;
    	ans=((maxend-minend+1)*(ans))%mod;
    	start=mini;
    }
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    cout<<ans;
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli  ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 998244353;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast;
    lli n,m;cin>>n>>m;
    map<lli,lli> maxpos;
    vector<plli> a(n);
    flli(i,0,n-1)
    {
    	cin>>a[i].first;
    	a[i].second=i;
    	maxpos[a[i].first]=i+1;
    }
    sort(all(a));
    vlli b(m);
    bool flag=true;
    flli(i,0,m-1)
    {
    	cin>>b[i];
    	if(maxpos[b[i]]==0)
    	{
    		flag=false;
    	}
    }
    if(b[0]!=a[0].first)flag=false;
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    lli curr=0;
    lli start=a[0].second;
    lli minend=start;
    lli maxend=minend;
    lli ans=1;
    flli(i,0,m-1)
    {
    	if(start>=n){flag=false;break;}
    	if(i==m-1)
    	{
    		continue;//I don't have any choice since all left will be the last subarray
    	}
    	lli next=b[i+1];
    	while(curr<n && a[curr].first<next)
    	{
    		minend=max(minend,a[curr].second);
    		curr++;
    	}
    	//from atleast start to end would be in one subarray
    	maxend=max(maxend,minend);
    	lli mini=INF;
    	while(curr<n && a[curr].first==next)
    	{
   			if(a[curr].second>minend)
   			{
   				mini=min(mini,a[curr].second);
   				if(curr+1<n && a[curr+1].first==next)
   					maxend=max(maxend,a[curr].second);
   			}
   			curr++;
    	}
    	if(mini==INF)
    	{
    		flag=false;
    		break;
    	}
    	//cout<<b[i]<<" "<<maxend<<" "<<minend<<endl;
    	ans=((maxend-minend+1)*(ans))%mod;
    	start=max(start,mini);
    	minend=max(minend,start);
    }
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    cout<<ans;
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli  ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 998244353;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast;
    lli n,m;cin>>n>>m;
    map<lli,lli> maxpos;
    vector<plli> a(n);
    flli(i,0,n-1)
    {
    	cin>>a[i].first;
    	a[i].second=i;
    	maxpos[a[i].first]=i+1;
    }
    sort(all(a));
    vlli b(m);
    bool flag=true;
    flli(i,0,m-1)
    {
    	cin>>b[i];
    	if(maxpos[b[i]]==0)
    	{
    		flag=false;
    	}
    }
    if(b[0]!=a[0].first)flag=false;
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    lli curr=0;
    lli start=a[0].second;
    lli ans=1;
    flli(i,0,m-1)
    {
    	if(i==m-1)
    	{
    		if(start>=n)flag=false;
    		continue;//I don't have any choice since all left will be the last subarray
    	}
    	lli next=b[i+1];
    	lli minend=start;
    	while(curr<n && a[curr].first<next)
    	{
    		minend=max(minend,a[curr].second);
    		curr++;
    	}
    	//from atleast start to end would be in one subarray
    	lli maxend=minend;
    	lli mini=INF;
    	while(curr<n && a[curr].first==next)
    	{
   			if(a[curr].second>minend)
   			{
   				mini=min(mini,a[curr].second);
   				if(curr+1<n && a[curr+1].first==next)
   					maxend=max(maxend,a[curr].second);
   			}
   			curr++;
    	}
    	if(mini==INF)
    	{
    		flag=false;
    		break;
    	}
    	//cout<<b[i]<<" "<<maxend<<" "<<minend<<endl;
    	ans=((maxend-minend+1)*(ans))%mod;
    	start=mini;
    }
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    cout<<ans;
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli  ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 998244353;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast;
    lli n,m;cin>>n>>m;
    map<lli,lli> maxpos;
    vector<plli> a(n);
    flli(i,0,n-1)
    {
    	cin>>a[i].first;
    	a[i].second=i;
    	maxpos[a[i].first]=i+1;
    }
    sort(all(a));
    vlli b(m);
    bool flag=true;
    flli(i,0,m-1)
    {
    	cin>>b[i];
    	if(maxpos[b[i]]==0)
    	{
    		flag=false;
    	}
    }
    if(b[0]!=a[0].first)flag=false;
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    lli curr=0;
    lli start=0;
    lli minend;
    lli maxend;
    lli ans=1;
    flli(i,0,m-1)
    {
    	if(maxpos[b[i]]==0 || maxpos[b[i]]<start){flag=false;break;}
    	if(i==m-1)
    	{
    		continue;
    	}
    	lli next=b[i+1];
    	minend=maxpos[b[i]];
    	while(curr<n && a[curr].first<next)
    	{
    		minend=max(minend,maxpos[a[curr].first]);
    		curr++;
    	}
    	maxend=maxpos[next]-1;
    	//cout<<b[i]<<" "<<maxend<<" "<<minend<<endl;
    	start=minend;
    	ans=((maxend-minend+1)*(ans))%mod;
    }
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    cout<<ans;
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
#include<algorithm>
#include<cmath>
#include<climits>
using namespace std;
typedef long long int lli;
typedef vector<int> vi;
typedef vector<long long int> vlli;
typedef pair<int,int> pii;
typedef pair<long long int,long long int> plli;
typedef vector< vi > vvi ;
typedef vector< vlli > vvlli  ;
#define fi(i,a,b) for(int i=a;i<=b;i++)
#define flli(i,a,b) for(long long int i=a;i<=b;i++)
#define bi(i,a,b) for(int i=a;i>=b;i--)
#define blli(i,a,b) for(long long int i=a;i>=b;i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define all(x) x.begin(),x.end()
#define sz(x) x.size()
#define pi 2*acos(0.0)
#define pb push_back
#define tr(v,it) for(decltype(v.begin()) it=v.begin();it!=v.end();it++)
#define present(v,num) (v.find(num)!=v.end())
#define cpresent(v,num) (find(v.begin(),v.end(),num)!=v.end())
#define pq priority_queue
#define mp make_pair
const int inf=INT_MAX;
const lli INF =LLONG_MAX;
const lli mod = 998244353;
int main() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    fast;
    lli n,m;cin>>n>>m;
    map<lli,lli> maxpos;
    vector<plli> a(n);
    flli(i,0,n-1)
    {
    	cin>>a[i].first;
    	a[i].second=i;
    	maxpos[a[i].first]=i+1;
    }
    sort(all(a));
    vlli b(m);
    bool flag=true;
    flli(i,0,m-1)
    {
    	cin>>b[i];
    	if(maxpos[b[i]]==0)
    	{
    		flag=false;
    	}
    }
    if(b[0]!=a[0].first)flag=false;
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    lli curr=0;
    lli start=a[0].second;
    lli minend=start;
    lli maxend=minend;
    lli ans=1;
    flli(i,0,m-1)
    {
    	if(start>=n){flag=false;break;}
    	if(i==m-1)
    	{
    		continue;//I don't have any choice since all left will be the last subarray
    	}
    	lli next=b[i+1];
    	while(curr<n && a[curr].first<next)
    	{
    		minend=max(minend,a[curr].second);
    		curr++;
    	}
    	//from atleast start to end would be in one subarray
    	maxend=max(maxend,minend);
    	lli mini=INF;
    	while(curr<n && a[curr].first==next)
    	{
   			if(a[curr].second>minend)
   			{
   				mini=min(mini,a[curr].second);
   				if(curr+1<n && a[curr+1].first==next)
   					maxend=max(maxend,a[curr].second);
   			}
   			curr++;
    	}
    	if(mini==INF)
    	{
    		flag=false;
    		break;
    	}
    	//cout<<b[i]<<" "<<maxend<<" "<<minend<<endl;
    	ans=((maxend-minend+1)*(ans))%mod;
    	start=max(start,mini);
    }
    if(!flag)
    {
    	cout<<0;
    	return 0;
    }
    cout<<ans;
    cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
const int oo=1e9;
const int Mod=998244353;
const long long OO=1e18;
const int N=1e5+5;
int n, m, a[N], b[N];
map<int,int>mp;
int ok(int l, int r, int val)
{
  for(int i=l; i<=r; i++)
    if(a[i]<val)return 0;
  return 1;
}
void xuat()
{
  cout<<0;
  exit(0);
}
void in()
{
  cin>>n>>m;
  for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
  for(int i=1; i<=m; i++)
  {
    cin>>b[i];
    if(!mp[b[i]])xuat();
  }
}
///////////////////85<3/////////////////
void solve()
{
  if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[n]],n,b[n])))xuat();
  for(int i=2 ; i<=n; i++)
    if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
  int res=1;
  for(int i=2; i<=m; i++)
  {
    int r = mp[b[i-1]]+1;
    for(int j=r; j<mp[b[i]] ; j++)
      if(a[j] < a[mp[b[i]]])r=j+1;
    res=(res*(mp[b[i]]-r+1)) % Mod;
  }
  cout<<res;
}
///////////////////85<3/////////////////
 main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
 // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
    in();
    solve();
}


#include<bits/stdc++.h>
using namespace std;
//#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
const int N=2e5+5;
int n,m,trai[N],vitri[N], a[N], b[N];
map<int,int>mp;
const int oo=1e9;
const int Mod=998244353;
const long long OO=1e18;
void xuat()
{
  cout << 0;
  exit(0);
}
void in()
{
    cin >> n >> m;
    for(int i = 1 ; i <= n ; ++i)
    {
      cin >> a[i];
      mp[a[i]] = i;
    }
    for(int i = 1 ; i <= m ; ++i)
    {
      cin >> b[i];
      vitri[i] = mp[b[i]];
      if(vitri[i] < vitri[i - 1]) xuat();
      if(mp[b[i]] == 0) xuat();
      trai[i] = vitri[i - 1] + 1;
    }
}
void pre()
{
  b[0] = b[1] - 1;
    int i = 1 , j = 1;
    while(i <= n)
    {
      if(a[i] < b[j]) trai[j] = i + 1;
      if(a[i] <= b[j - 1])xuat();
      if(i == vitri[j]) j++;
      i++;
    }
}
void solve()
{
    pre();
    int ans = 1;
    for(int i = 2 ; i <= m ; ++i) ans = (ans * (vitri[i] - trai[i] + 1)) % Mod;
    cout << ans;
}
///////////////////85<3/////////////////
int main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
 // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
    in();
    solve();
}


  #include<bits/stdc++.h>
  using namespace std;
  #define int long long
  #define ii pair<int,int>
  #define fi first
  #define se second
  #define pb push_back
  //#define mp make_pair
  const int oo=1e9;
  const int Mod=998244353;
  const long long OO=1e18;
  const int N=2e5+5;
  int n, m, a[N], b[N];
  map<int,int>mp;
  int ok(int l, int r, int val)
  {
    for(int i=l; i<=r; i++)
      if(a[i]<val)return 0;
    return 1;
  }
  void xuat()
  {
    cout<<0;
    exit(0);
  }
  void in()
  {
    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
    for(int i=1; i<=m; i++)
    {
      cin>>b[i];
      if(!mp[b[i]])xuat();
    }
  }
  ///////////////////85<3/////////////////
  void solve()
  {
    if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[m]],n,b[m])))xuat();
    for(int i=2 ; i<=m; i++)
      if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
    int res=1;
    for(int i=2; i<=m; i++)
    {
      int r = mp[b[i-1]]+1;
      if(r>mp[b[i]])xuat();
      for(int j=r; j<mp[b[i]] ; j++)
        if(a[j] < a[mp[b[i]]])r=j+1;
      res=(res*(mp[b[i]]-r+1)) % Mod;
    }
    cout<<res;
  }
  ///////////////////85<3/////////////////
   main()
  {
    if(fopen("trash.inp", "r"))
      freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
   // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
      in();
      solve();
  }


  #include<bits/stdc++.h>
  using namespace std;
  //#define int long long
  #define ii pair<int,int>
  #define fi first
  #define se second
  #define pb push_back
  //#define mp make_pair
  const int oo=1e9;
  const int Mod=998244353;
  const long long OO=1e18;
  const int N=2e5+5;
  int n, m, a[N], b[N];
  map<int,int>mp;
  int ok(int l, int r, int val)
  {
    for(int i=l; i<=r; i++)
      if(a[i]<val)return 0;
    return 1;
  }
  void xuat()
  {
    cout<<0;
    exit(0);
  }
  void in()
  {
    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
    for(int i=1; i<=m; i++)
    {
      cin>>b[i];
      if(!mp[b[i]])xuat();
    }
  }
  ///////////////////85<3/////////////////
  void solve()
  {
    if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[n]],n,b[n])))xuat();
    for(int i=2 ; i<=n; i++)
      if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
    int res=1;
    for(int i=2; i<=m; i++)
    {
      int r = mp[b[i-1]]+1;
      for(int j=r; j<mp[b[i]] ; j++)
        if(a[j] < a[mp[b[i]]])r=j+1;
      res=(res*(mp[b[i]]-r+1)) % Mod;
    }
    cout<<res;
  }
  ///////////////////85<3/////////////////
   main()
  {
    if(fopen("trash.inp", "r"))
      freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
   // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
      in();
      solve();
  }


  #include<bits/stdc++.h>
  using namespace std;
  //#define int long long
  #define ii pair<int,int>
  #define fi first
  #define se second
  #define pb push_back
  //#define mp make_pair
  const int oo=1e9;
  const int Mod=998244353;
  const long long OO=1e18;
  const int N=2e5+5;
  int n, m, a[N], b[N];
  map<int,int>mp;
  int ok(int l, int r, int val)
  {
    for(int i=l; i<=r; i++)
      if(a[i]<val)return 0;
    return 1;
  }
  void xuat()
  {
    cout<<0;
    exit(0);
  }
  void in()
  {
    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
    for(int i=1; i<=m; i++)
    {
      cin>>b[i];
      if(!mp[b[i]])xuat();
    }
  }
  ///////////////////85<3/////////////////
  void solve()
  {
    if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[n]],n,b[n])))xuat();
    for(int i=2 ; i<=m; i++)
      if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
    int res=1;
    for(int i=2; i<=m; i++)
    {
      int r = mp[b[i-1]]+1;
      if(r>mp[b[i]])xuat();
      for(int j=r; j<mp[b[i]] ; j++)
        if(a[j] < a[mp[b[i]]])r=j+1;
      res=(res*(mp[b[i]]-r+1)) % Mod;
    }
    cout<<res;
  }
  ///////////////////85<3/////////////////
   main()
  {
    if(fopen("trash.inp", "r"))
      freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
   // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
      in();
      solve();
  }


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define fi first
#define se second
#define pb push_back
const int N=2e5+5;
int n,m,trai[N],vitri[N], a[N], b[N];
map<int,int>mp;
const int oo=1e9;
const int Mod=998244353;
const long long OO=1e18;
void xuat()
{
  cout << 0;
  exit(0);
}
void in()
{
    cin >> n >> m;
    for(int i = 1 ; i <= n ; ++i)
    {
      cin >> a[i];
      mp[a[i]] = i;
    }
    for(int i = 1 ; i <= m ; ++i)
    {
      cin >> b[i];
      vitri[i] = mp[b[i]];
      if(vitri[i] < vitri[i - 1]) xuat();
      if(mp[b[i]] == 0) xuat();
      trai[i] = vitri[i - 1] + 1;
    }
}
void pre()
{
  b[0] = b[1] - 1;
    int i = 1 , j = 1;
    while(i <= n)
    {
      if(a[i] <= b[j - 1])xuat();
      if(a[i] < b[j]) trai[j] = i + 1;
      if(i == vitri[j]) j++;
      i++;
    }
}
void solve()
{
    pre();
    int ans = 1;
    for(int i = 2 ; i <= m ; ++i) ans = (ans * (vitri[i] - trai[i] + 1)) % Mod;
    cout << ans;
}
///////////////////85<3/////////////////
 main()
{
  if(fopen("trash.inp", "r"))
    freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
 // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
    in();
    solve();
}


  #include<bits/stdc++.h>
  using namespace std;
  //#define int long long
  #define ii pair<int,int>
  #define fi first
  #define se second
  #define pb push_back
  //#define mp make_pair
  const int oo=1e9;
  const int Mod=998244353;
  const long long OO=1e18;
  const int N=1e5+5;
  int n, m, a[N], b[N];
  map<int,int>mp;
  int ok(int l, int r, int val)
  {
    for(int i=l; i<=r; i++)
      if(a[i]<val)return 0;
    return 1;
  }
  void xuat()
  {
    cout<<0;
    exit(0);
  }
  void in()
  {
    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
    for(int i=1; i<=m; i++)
    {
      cin>>b[i];
      if(!mp[b[i]])xuat();
    }
  }
  ///////////////////85<3/////////////////
  void solve()
  {
    if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[n]],n,b[n])))xuat();
    for(int i=2 ; i<=n; i++)
      if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
    int res=1;
    for(int i=2; i<=m; i++)
    {
      int r = mp[b[i-1]]+1;
      for(int j=r; j<mp[b[i]] ; j++)
        if(a[j] < a[mp[b[i]]])r=j+1;
      res=(res*(mp[b[i]]-r+1)) % Mod;
    }
    cout<<res;
  }
  ///////////////////85<3/////////////////
   main()
  {
    if(fopen("trash.inp", "r"))
      freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
   // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
      in();
      solve();
  }


  #include<bits/stdc++.h>
  using namespace std;
  //#define int long long
  #define ii pair<int,int>
  #define fi first
  #define se second
  #define pb push_back
  //#define mp make_pair
  const int oo=1e9;
  const int Mod=998244353;
  const long long OO=1e18;
  const int N=2e5+5;
  int n, m, a[N], b[N];
  map<int,int>mp;
  int ok(int l, int r, int val)
  {
    for(int i=l; i<=r; i++)
      if(a[i]<val)return 0;
    return 1;
  }
  void xuat()
  {
    cout<<0;
    exit(0);
  }
  void in()
  {
    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
    for(int i=1; i<=m; i++)
    {
      cin>>b[i];
      if(!mp[b[i]])xuat();
    }
  }
  ///////////////////85<3/////////////////
  void solve()
  {
    if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[n]],n,b[n])))xuat();
    for(int i=2 ; i<=n; i++)
      if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
    int res=1;
    for(int i=2; i<=m; i++)
    {
      int r = mp[b[i-1]]+1;
      if(r>mp[b[i]])xuat();
      for(int j=r; j<mp[b[i]] ; j++)
        if(a[j] < a[mp[b[i]]])r=j+1;
      res=(res*(mp[b[i]]-r+1)) % Mod;
    }
    cout<<res;
  }
  ///////////////////85<3/////////////////
   main()
  {
    if(fopen("trash.inp", "r"))
      freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
   // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
      in();
      solve();
  }


  #include<bits/stdc++.h>
  using namespace std;
  #define int long long
  #define ii pair<int,int>
  #define fi first
  #define se second
  #define pb push_back
  //#define mp make_pair
  const int oo=1e9;
  const int Mod=998244353;
  const long long OO=1e18;
  const int N=2e5+5;
  int n, m, a[N], b[N];
  map<int,int>mp;
  int ok(int l, int r, int val)
  {
    for(int i=l; i<=r; i++)
      if(a[i]<val)return 0;
    return 1;
  }
  void xuat()
  {
    cout<<0;
    exit(0);
  }
  void in()
  {
    cin>>n>>m;
    for(int i=1; i<=n; i++)cin>>a[i], mp[a[i]]=i;
    for(int i=1; i<=m; i++)
    {
      cin>>b[i];
      if(!mp[b[i]])xuat();
    }
  }
  ///////////////////85<3/////////////////
  void solve()
  {
    if((!ok(1,mp[b[1]],b[1])) || (!ok(mp[b[n]],n,b[n])))xuat();
    for(int i=2 ; i<=m; i++)
      if(!ok(mp[b[i-1]], mp[b[i]], b[i-1]))xuat();
    int res=1;
    for(int i=2; i<=m; i++)
    {
      int r = mp[b[i-1]]+1;
      if(r>mp[b[i]])xuat();
      for(int j=r; j<mp[b[i]] ; j++)
        if(a[j] < a[mp[b[i]]])r=j+1;
      res=(res*(mp[b[i]]-r+1)) % Mod;
    }
    cout<<res;
  }
  ///////////////////85<3/////////////////
   main()
  {
    if(fopen("trash.inp", "r"))
      freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
   // else freopen("trash.inp", "r", stdin), freopen("trash.out", "w", stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
      in();
      solve();
  }


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 998244353;

unordered_map<int, int> x_id;
int n, m;
int nxx[maxn];
int mxx[maxn];
int bxx[2 * maxn];
int bmm[2 * maxn];
int nmm[2 * maxn];
int mLR[maxn];

void input()
{
    scanf("%d%d", &n, &m);
    for(int i = 1;i <= n;i++)
        scanf("%d", nxx + i);
    for(int i = 1;i <= m;i++)
        scanf("%d", mxx + i);
}

void ToId()
{
    int totId = 0;

    for(int i = 1;i <= n;i++)
        bxx[++totId] = nxx[i];
    for(int i = 1;i <= m;i++)
        bxx[++totId] = mxx[i];

    sort(bxx + 1, bxx + 1 + totId);
    int sumId = unique(bxx + 1, bxx + 1 + totId) - (bxx + 1);

    for(int i = 1;i <= sumId;i++)
        x_id[bxx[i]] = i;
}

void getBm()
{
    int nowId = n + m + 1;

    for(int i = n;i >= 0;i--)
    {
        if(x_id[nxx[i]] < nowId)
        {
            bmm[nowId] = i + 1;
            nowId = x_id[nxx[i]];
            nmm[nowId] = i;

        }
    }
}

bool chkBad()
{
    int preId = x_id[mxx[1]];

    for(int id = 1;id < preId;id++)
    {
        if(bmm[id]) return 1;
    }
    return 0;
}

int calBm()
{
    int res = 1;

    if(chkBad()) return 0;
    for(int i = 1;i <= m - 1;i++)
    {
        int id = x_id[mxx[i]];
        int NxId = x_id[mxx[i + 1]];

        //printf("i=%d id=%d NxId=%d: \n", i, id, NxId);
        if(bmm[id] == 0 || bmm[NxId] == 0)
            return 0;

        int L = bmm[NxId] - 1;
        int R = nmm[NxId] - 1;

        res = 1ll * res * (R - L + 1) % mod;
    }
    return res;
}

void out_debug()
{
    for(int i = 1;i <= n;i++)
        printf("id: nxx[%d] = %d\n", i, x_id[nxx[i]]);
    for(int i = 1;i <= m;i++)
        printf("id: mxx[%d] = %d\n", i, x_id[mxx[i]]);
    for(int i = 1;i <= m + n;i++)
        printf("bxx[%d] = %d\n", i, bxx[i]);
}

int main()
{
    bool debug = 0;

    input();
    ToId();
    getBm();
    int res = calBm();

    if(debug) out_debug();
    printf("%d\n", res);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <unordered_map>
using namespace std;
const int maxn = 2e5 + 5;
const int mod = 998244353;

unordered_map<int, int> x_id;
int n, m;
int nxx[maxn];
int mxx[maxn];
int bxx[2 * maxn];
int bmm[2 * maxn];
int nmm[2 * maxn];
int mLR[maxn];

void input()
{
    scanf("%d%d", &n, &m);
    for(int i = 1;i <= n;i++)
        scanf("%d", nxx + i);
    for(int i = 1;i <= m;i++)
        scanf("%d", mxx + i);
}

void ToId()
{
    int totId = 0;

    for(int i = 1;i <= n;i++)
        bxx[++totId] = nxx[i];
    for(int i = 1;i <= m;i++)
        bxx[++totId] = mxx[i];

    sort(bxx + 1, bxx + 1 + totId);
    int sumId = unique(bxx + 1, bxx + 1 + totId) - (bxx + 1);

    for(int i = 1;i <= sumId;i++)
        x_id[bxx[i]] = i;
}

void getBm()
{
    int nowId = n + m + 1;

    for(int i = n;i >= 0;i--)
    {
        if(x_id[nxx[i]] < nowId)
        {
            bmm[nowId] = i + 1;
            nowId = x_id[nxx[i]];
            nmm[nowId] = i;

        }
    }
}

bool chkBad()
{
    int preId = x_id[mxx[1]];

    for(int id = 1;id < preId;id++)
    {
        if(bmm[id]) return 1;
    }
    if(!bmm[preId]) return 1;
    return 0;
}

int calBm()
{
    int res = 1;

    if(chkBad()) return 0;
    for(int i = 1;i <= m - 1;i++)
    {
        int id = x_id[mxx[i]];
        int NxId = x_id[mxx[i + 1]];

        //printf("i=%d id=%d NxId=%d: \n", i, id, NxId);
        if(bmm[id] == 0 || bmm[NxId] == 0)
            return 0;

        int L = bmm[NxId] - 1;
        int R = nmm[NxId] - 1;

        res = 1ll * res * (R - L + 1) % mod;
    }
    return res;
}

void out_debug()
{
    for(int i = 1;i <= n;i++)
        printf("id: nxx[%d] = %d\n", i, x_id[nxx[i]]);
    for(int i = 1;i <= m;i++)
        printf("id: mxx[%d] = %d\n", i, x_id[mxx[i]]);
    for(int i = 1;i <= m + n;i++)
        printf("bxx[%d] = %d\n", i, bxx[i]);
}

int main()
{
    bool debug = 0;

    input();
    ToId();
    getBm();
    int res = calBm();

    if(debug) out_debug();
    printf("%d\n", res);
    return 0;
}