#include <bits/stdc++.h>
using namespace std;
long long MOD = 998244353;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(18);
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(m);
  for (long long i = 0; i < n; i++) cin >> a[i];
  for (long long i = 0; i < m; i++) cin >> b[i];
  for (long long i = 0; i < n; i++) {
    if (a[i] < b[0]) {
      cout << 0 << endl;
      return 0;
    }
  }
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    v[i] = (upper_bound(b.begin(), b.end(), a[i]) - b.begin()) - 1;
  }
  for (long long i = n - 2; i >= 0; i--) {
    v[i] = min(v[i], v[i + 1]);
  }
  long long cur = 0;
  for (long long i = 0; i < n; i++) {
    if (v[i] == cur)
      cur++;
    else if (v[i] > cur) {
      cout << 0 << endl;
      return 0;
    }
  }
  if (cur < m) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 1;
  long long l = 0, r = 0;
  cur = 0;
  for (long long i = 0; i < n; i++) {
    if (v[i] > cur) {
      r = i - 1;
      long long check = -1;
      for (long long j = l; j <= r; j++) {
        if (a[j] == b[cur]) check = j;
      }
      if (check == -1) {
        cout << 0 << endl;
        return 0;
      }
      if (cur > 0) {
        ans *= check - l + 1;
        ans %= MOD;
      }
      l = i;
      cur++;
    }
  }
  long long check = -1;
  for (long long j = l; j <= n - 1; j++) {
    if (a[j] == b[cur]) check = j;
  }
  if (cur > 0) {
    ans *= check - l + 1;
    ans %= MOD;
  }
  if (check == -1)
    cout << 0 << endl;
  else
    cout << ans << endl;
}
