#include <bits/stdc++.h>
using namespace std;
long long ans = 1, res, temp;
long long a[500000], b[500000];
signed main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> a[n - 1 - i];
  for (long long i = 0; i < m; i++) cin >> b[m - 1 - i];
  long long it = 0, last = -1;
  bool flag = 0;
  for (long long i = 0; i < n; i++) {
    if (it == m - 1) {
      long long mn = INT_MAX;
      for (long long j = i; j < n; j++) mn = min(mn, a[j]);
      if (mn != b[it]) {
        cout << 0 << endl;
        exit(0);
      }
      break;
    }
    if (a[i] < b[it]) {
      cout << 0 << endl;
      exit(0);
    }
    if (a[i] == b[it]) {
      long long k = i + 1, next = -1;
      while (k < n and a[k] >= b[it]) {
        if (a[k] == b[it + 1]) next = k;
        k++;
      }
      if (next == -1) {
        next = k;
        while (next < n and a[next] != b[it + 1]) next++;
      }
      if (next == n) {
        cout << 0 << endl;
        exit(0);
      }
      ans = ((k - i) * ans) % 998244353;
      i = k - 1;
      it++;
      last = k;
    }
  }
  cout << ans << endl;
  return 0;
}
