#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
void Solve() {
  long long n, m;
  cin >> n >> m;
  long long a[n + 1], b[m + 1], AtLeast[m + 1];
  map<long long, long long> occur;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i];
    occur[a[i]] = i;
  }
  long long lastoccur = 0;
  for (long long i = 1; i <= m; i++) {
    cin >> b[i];
    AtLeast[i] = -1;
  }
  for (long long i = 1; i <= m; i++) {
    if (!occur.count(b[i]) || lastoccur > occur[b[i]]) {
      cout << "0\n";
      return;
    }
    lastoccur = occur[b[i]];
  }
  for (long long i = 1; i <= occur[b[1]]; i++) {
    if (a[i] < b[1]) {
      cout << "0\n";
      return;
    }
  }
  long long cur_pos = 2;
  for (long long i = occur[b[1]] + 1; i <= n; i++) {
    if (a[i] < b[cur_pos - 1]) {
      cout << "0\n";
      return;
    }
    if (a[i] < b[cur_pos]) {
      AtLeast[cur_pos - 1] = i;
    }
    if (occur[b[cur_pos]] == i) {
      cur_pos++;
      if (cur_pos == m + 1) break;
    }
  }
  for (long long i = occur[b[m]] + 1; i <= n; i++) {
    if (a[i] < b[m]) {
      cout << "0\n";
      return;
    }
  }
  if (m == 1) {
    cout << "1\n";
    return;
  }
  if (m == 2) {
    cout << occur[b[2]] - occur[b[1]] << "\n";
    return;
  }
  long long ans = 1;
  for (long long i = 1; i < m; i++) {
    if (AtLeast[i] == -1) {
      long long len = occur[b[i + 1]] - occur[b[i]];
      ans *= len;
      ans %= MOD;
    } else {
      long long len = occur[b[i + 1]] - AtLeast[i];
      ans *= len;
      ans %= MOD;
    }
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  Solve();
  return 0;
}
