#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
void read(int& val) {
  int x = 0;
  int bz = 1;
  char c;
  for (c = getchar(); (c < '0' || c > '9') && c != '-'; c = getchar())
    ;
  if (c == '-') {
    bz = -1;
    c = getchar();
  }
  for (; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  val = x * bz;
}
const int mod = 998244353;
const int maxn = 2e6 + 7;
using namespace std;
int n, m;
int a[maxn], b[maxn];
int main() {
  read(n);
  read(m);
  for (int i = 1; i <= n; i++) read(a[i]);
  for (int i = 1; i <= m; i++) read(b[i]);
  reverse(a + 1, a + 1 + n);
  reverse(b + 1, b + 1 + m);
  long long ans = 1;
  int pos = 1;
  while (a[pos] > b[1] && pos <= n) {
    pos++;
  }
  if (pos > n || a[pos] != b[1]) {
    puts("0");
    return 0;
  }
  int l = 2;
  int mi = b[1];
  while (1) {
    int lstpos = pos;
    if (l > m) {
      for (int i = pos; i <= n; i++) {
        if (a[i] < b[m]) return 0 * puts("0");
      }
      break;
    }
    int flag = 1;
    while (mi != b[l] && pos <= n) {
      pos++;
      mi = min(a[pos], mi);
      if (a[pos] < b[l - 1] && flag) {
        ans = ans * (pos - lstpos) % mod;
        flag = 0;
      }
    }
    if (pos > n) {
      return 0 * puts("0");
    }
    l++;
  }
  if (l <= m) ans = 0;
  printf("%lld\n", ans);
  return 0;
}
