#include <bits/stdc++.h>
using namespace std;
const int P = 998244353;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), c(m, -1), d(m), bb(m);
  map<int, int> b;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int j = 0; j < m; j++) {
    int q;
    cin >> q;
    b.insert(make_pair(q, j));
    bb[j] = q;
  }
  for (int k = 0; k < n; k++) {
    if (b.find(a[k]) != b.end()) {
      c[b.find(a[k])->second] = k;
    }
  }
  if (c[0] == -1) {
    cout << 0;
    return 0;
  }
  for (int l = 1; l < m; l++) {
    if ((c[l] < c[l - 1]) || (c[l - 1] == -1)) {
      cout << 0;
      return 0;
    }
  }
  int s = 0, t = 0;
  d[0] = c[0];
  while (s <= c[t]) {
    if (t >= 1) {
      if (a[s] < bb[t - 1]) {
        cout << 0;
        return 0;
      }
    }
    if (a[s] < bb[t]) d[t] = s;
    if (s == c[t]) {
      t++;
      if (t >= m) break;
      d[t] = c[t - 1];
    }
    s++;
  }
  if (d[0] < c[0]) {
    cout << 0;
    return 0;
  }
  for (int e = c[m - 1]; e < n; e++) {
    if (a[e] < bb[m - 1]) {
      cout << 0;
      return 0;
    };
  }
  if (m == 1) {
    cout << 1;
    return 0;
  }
  long long int sum = 1;
  for (int w = 1; w < m; w++) {
    sum = (sum * (c[w] - d[w])) % P;
  }
  cout << sum;
}
