#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  long long int a[n], b[m];
  for (int i = 0; i < n; i++) cin >> a[n - i - 1];
  for (int i = 0; i < m; i++) cin >> b[m - i - 1];
  bool ans = true;
  bool equal = true;
  int index = 0, loc = 0;
  int count = 0;
  vector<long long int> v;
  while (index < n && loc < m) {
    if (equal) {
      if (a[index] == b[loc]) {
        count = 1;
        index++;
        equal = false;
      } else if (a[index] > b[loc])
        index++;
      else {
        ans = false;
        break;
      }
    } else {
      if (a[index] >= b[loc]) {
        index++;
        count++;
      } else {
        v.push_back(count);
        count = 0;
        equal = true;
        loc++;
      }
    }
  }
  if (v.size() != (m - 1)) ans = false;
  if (!ans)
    cout << 0 << "\n";
  else {
    for (int i = 0; i < n; i++) {
      if (a[i] < b[m - 1]) {
        ans = false;
        break;
      }
    }
    bool found = false;
    for (int i = 0; i < n; i++) {
      if (a[i] == b[m - 1]) {
        found = true;
        break;
      }
    }
    if (!found) ans = false;
    if (!ans)
      cout << 0 << "\n";
    else {
      long long int ans = 1;
      long long int z = 998244353;
      for (int i = 0; i < v.size(); i++) ans = (ans * v[i]) % z;
      cout << ans << "\n";
    }
  }
  return 0;
}
