#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
const int oo = 998244353;
int n, m;
int a[N], b[N];
pair<int, int> c[N];
long long f[N];
int lt[N], suffix_min[N];
int pos[N];
stack<pair<int, int> > m1;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%d", a + i);
    c[i] = {a[i], i};
  }
  sort(c + 1, c + n + 1);
  for (int i = 1; i <= m; ++i) scanf("%d", b + i);
  for (int i = 1, cur = 1; i <= n; ++i) {
    while (cur <= m && b[cur] < c[i].first) cur++;
    if (cur > m) break;
    pos[cur] = c[i].second;
  }
  for (int i = 1; i <= n; ++i) {
    while (!m1.empty() && m1.top().first >= a[i]) m1.pop();
    if (!m1.empty()) lt[i] = m1.top().second;
    m1.push({a[i], i});
  }
  suffix_min[n + 1] = 2e9;
  for (int i = n; i; --i) suffix_min[i] = min(suffix_min[i + 1], a[i]);
  long long ans = 0;
  for (int i = 1; i <= m; ++i) {
    int cur = pos[i];
    if (cur == 0 || suffix_min[cur] < b[i]) {
      ans = 0;
      break;
    }
    if (i == 1) {
      if (lt[cur] != 0) {
        ans = 0;
        break;
      }
      ans = 1;
      continue;
    }
    if (a[lt[cur]] < b[i - 1] || lt[cur] < pos[i - 1]) {
      ans = 0;
      break;
    }
    ans = ans * (pos[i] - lt[cur]) % oo;
  }
  printf("%lld\n", ans % oo);
  return 0;
}
