#include <bits/stdc++.h>
using namespace std;
template <typename T>
void uin(T &a, T b) {
  if (b < a) a = b;
}
template <typename T>
void uax(T &a, T b) {
  if (b > a) a = b;
}
const long long MOD = 998244353;
long long mod(long long x) {
  x %= MOD;
  if (x < 0) x += MOD;
  return x;
}
const long long N = 200 * 1000 + 228;
long long n, m;
long long a[N], b[N];
long long left228[N], right228[N];
long long suff[N];
map<long long, long long> last;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (long long i = 1; i <= m; ++i) {
    cin >> b[i];
  }
  if (m == 1) {
    for (long long i = 2; i <= n; ++i) {
      uin(a[1], a[i]);
    }
    if (a[1] != b[1]) {
      cout << 0 << '\n';
    } else {
      cout << 1 << endl;
    }
    return 0;
  }
  suff[m + 1] = n + 1;
  for (long long x = m; x; --x) {
    suff[x] = suff[x + 1];
    while (suff[x] - 1 >= 1 && a[suff[x] - 1] >= b[x]) {
      --suff[x];
    }
    if (suff[x] > n) {
      cout << 0 << '\n';
      return 0;
    }
  }
  if (suff[1] != 1) {
    cout << 0 << '\n';
    return 0;
  }
  for (long long x = 1; x < m; ++x) {
    if (suff[x] == suff[x + 1]) {
      cout << 0 << '\n';
      return 0;
    }
  }
  for (long long i = 1; i <= n; ++i) {
    last[a[i]] = i;
  }
  long long ans = 1;
  if (last[b[1]] == 0) {
    cout << 0 << endl;
    return 0;
  }
  for (long long i = 2; i <= m; ++i) {
    if (last[b[i]] == 0) {
      cout << 0 << '\n';
      return 0;
    }
    if (last[b[i]] - suff[i] < 0) {
      cout << 0 << '\n';
      return 0;
    }
    ans = mod(ans * (last[b[i]] - suff[i] + 1));
  }
  cout << ans << '\n';
  return 0;
}
