#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  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];
  if (m > n)
    cout << 0 << "\n";
  else {
    vector<int> mins;
    mins.push_back(a[0]);
    vector<vector<int> > indices(m);
    vector<int>::iterator itr;
    itr = lower_bound(b.begin(), b.end(), a[0]);
    if (*itr == a[0]) {
      int index = itr - b.begin();
      indices[index].push_back(0);
    }
    for (int i = 1; i < n; i++) {
      itr = lower_bound(b.begin(), b.end(), a[i]);
      if (*itr == a[i]) {
        int index = itr - b.begin();
        indices[index].push_back(i);
      }
      while (!mins.empty() && mins.back() >= a[i]) mins.pop_back();
      mins.push_back(a[i]);
    }
    long long int ans = 1;
    if (mins[0] != b[0])
      cout << 0 << "\n";
    else {
      int b_index = 1;
      int mins_index = 1;
      bool var = false;
      bool check = true;
      if (b_index >= m || mins_index >= mins.size()) var = true;
      while (!var) {
        int i1 = indices[b_index - 1].back();
        int i2 = indices[b_index].back();
        while (mins_index < mins.size() && mins[mins_index] < b[b_index])
          mins_index++;
        if (mins_index == mins.size()) {
          check = false;
          break;
        }
        if (mins[mins_index] != b[b_index]) {
          check = false;
          break;
        }
        int p = -1;
        for (int i = i1; i <= i2; i++) {
          if (a[i] == mins[mins_index - 1]) p = i;
        }
        ans = ans * (i2 - p);
        ans = ans % 998244353;
        b_index++;
        mins_index++;
        if (b_index >= m || mins_index >= mins.size()) var = true;
      }
      if (b_index == m && check)
        cout << ans << "\n";
      else
        cout << 0 << "\n";
    }
  }
}
