#include <bits/stdc++.h>
using namespace std;
const int NN = 1e7 + 7;
const int N = 5e5 + 7;
const int M = 26;
const long long mod = 998244353;
const long long inf = 1e18 + 7;
const double long rf = 1e-14;
const int B = sqrt(N);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, m;
int a[N];
int b[N];
int c[N];
int l[N];
int r[N];
int p[N];
void solve1() {
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i <= m; i++) cin >> b[i];
  c[n] = a[n];
  for (int i = n - 1; i >= 1; i--) {
    c[i] = min(a[i], c[i + 1]);
  }
  bool f = false;
  int j = m;
  for (int i = n; i >= 1; i--) {
    if (c[i] == b[j]) {
      j -= 1;
    }
  }
  if (j != 0 || c[1] != b[1]) {
    cout << 0;
    return;
  }
  vector<pair<int, int> > v;
  for (int i = n, j = m; i >= 1; i--) {
    if (c[i] == b[j]) {
      int h = i;
      while (h >= 1 && c[h] == c[i]) h -= 1;
      v.push_back({h + 1, i});
      i = h + 1;
      j -= 1;
    }
  }
  long long ans = 1;
  reverse(v.begin(), v.end());
  for (int i = 1; i < (int)v.size(); i++) {
    long long l = v[i].second - v[i].first + 1;
    ans = ans * 1ll * l % mod;
  }
  cout << ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int cghf = 1;
  while (cghf--) {
    solve1();
  }
}
