#include <bits/stdc++.h>
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N = 2e5 + 10;
const int mod = 998244353;
int a[N], b[N], p[N], l[N];
int getMin(int l, int r) {
  int x = a[l];
  for (int i = l; i <= r; i++) x = min(x, a[i]);
  return x;
}
void zero() {
  cout << 0;
  exit(0);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  int ptr = m - 1;
  for (int i = n - 1; i >= 0; i--) {
    if (a[i] == b[ptr]) {
      p[ptr--] = i;
    }
    if (ptr < 0) break;
  }
  if (ptr >= 0) {
    zero();
  }
  l[0] = -1;
  for (int i = m - 1; i >= 1; i--) {
    int j = p[i];
    while (b[i] <= a[j]) j--;
    l[i] = j;
  }
  if (getMin(0, p[0]) != b[0] || getMin(p[m - 1], n - 1) != b[m - 1]) zero();
  for (int i = 0; i < m - 1; i++) {
    if (getMin(p[i], p[i + 1]) != b[i]) zero();
  }
  long long ans = 1;
  for (int i = 1; i < m; i++) {
    ans *= (p[i] - l[i]);
    ans %= mod;
  }
  cout << ans;
  return 0;
}
