#include <bits/stdc++.h>
using namespace std;
const long double PI = 3.14159265358979323;
const long long MOD = 998244353;
const int MXN = 2e6 + 500;
const double EPS = 1e-6;
int n, m;
vector<int> a, b, ch, la;
int bins(int x) {
  int l = 0, r = m + 1, md;
  while (l + 1 < r) {
    md = (l + r) / 2;
    if (b[md] > x)
      r = md;
    else
      l = md;
  }
  return l;
}
int main() {
  {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
  };
  int k;
  long long ans = 1;
  cin >> n >> m;
  a.resize(n);
  b.resize(m + 1);
  ch.resize(m + 1);
  for (int i = 0; i < n; i++) cin >> a[i];
  b[0] = -1;
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    ch[i] = -1;
  }
  if (n < m) {
    cout << "0\n";
    return 0;
  }
  for (int i = 0; i < n; i++) {
    k = bins(a[i]);
    if (a[i] == b[k]) ch[k] = i;
  }
  for (int i = 1; i <= m; i++)
    if (ch[i] == -1) {
      cout << "0\n";
      return 0;
    }
  for (int i = 2; i <= m; i++) {
    if (ch[i] < ch[i - 1]) {
      cout << "0\n";
      return 0;
    }
  }
  for (int i = 0; i < ch[1]; i++)
    if (a[i] < b[1]) {
      cout << "0\n";
      return 0;
    }
  for (int i = ch[m] + 1; i < n; i++) {
    if (a[i] < b[m]) {
      cout << "0\n";
      return 0;
    }
  }
  long long c = 1, cnt;
  for (int j = 1; j < m; j++) {
    c = 1;
    cnt = 1;
    for (int i = ch[j + 1] - 1; i > ch[j]; i--) {
      if (a[i] < b[j]) {
        cout << "0\n";
        return 0;
      }
      if (c && a[i] >= b[j + 1])
        cnt++;
      else
        c = 0;
    }
    if (cnt != 1) ans = (ans * cnt) % MOD;
  }
  if (!ans) ans++;
  cout << ans << '\n';
  return 0;
}
