#include <bits/stdc++.h>
using namespace std;
const int inf = 2e9;
const double eps = 1e-9;
const int mod = 998244353;
int n, m, x;
void solve(int tc) {
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
  }
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  int ans = 1;
  int ai = 0, bi = 0;
  while (ai < n and bi < m) {
    while (ai < n and a[ai] > b[bi]) {
      ++ai;
    }
    if (a[ai] != b[bi]) {
      cout << "0\n";
      return;
    }
    int ind = ai;
    while (ai < n and a[ai] >= b[bi]) {
      ++ai;
    }
    ++bi;
    if (bi == m) break;
    ans = (1LL * ans * (ai - ind)) % mod;
  }
  if (bi < m or ai < n) {
    cout << "0\n";
    return;
  }
  cout << ans << "\n";
}
int main() {
  int tt = 1;
  for (int i = 1; i <= tt; ++i) {
    solve(i);
  }
  return 0;
}
