#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int mod = 998244353;
int n, m, a[maxn], b[maxn];
int l[maxn], r[maxn], st, ed;
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", a + i);
  for (int i = 1; i <= m; i++) scanf("%d", b + i);
  int j = n;
  for (int i = m; i; i--) {
    for (; j; j--) {
      if (a[j] == b[i]) {
        r[i] = j;
        break;
      }
    }
  }
  for (int i = r[m]; i <= n; i++)
    if (a[i] < b[m]) return puts("0"), 0;
  for (int i = 1; i <= r[1]; i++)
    if (a[i] < b[1]) return puts("0"), 0;
  l[1] = st = r[1], ed = r[m];
  for (int i = 2; i <= m; i++) {
    l[i] = r[i - 1] + 1;
    for (int j = r[i - 1] + 1; j < r[i]; j++)
      if (a[j] < b[i]) l[i] = j + 1;
    for (int j = r[i - 1]; j < l[i]; j++)
      if (a[j] < b[i - 1]) return puts("0"), 0;
  }
  long long ans = 1LL;
  for (int i = 1; i <= m; i++) ans = ans * (r[i] - l[i] + 1) % mod;
  printf("%lld\n", ans);
  return 0;
}
