#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m), bf(m), bl(m);
  for (auto &v : a) cin >> v;
  for (auto &v : b) cin >> v;
  vector<pair<int, int> > p(n);
  for (int i = 0; i < n; i++) p[i] = {a[i], i};
  sort(p.begin(), p.end());
  long long ans = 1, bp = 0;
  for (int i = 0; i < n; i++)
    if (b[bp] == p[i].first)
      bl[bp] = p[i].second;
    else if (bp != m - 1 && b[bp + 1] == p[i].first)
      bl[++bp] = p[i].second;
  for (int i = 0; i < m - 1; i++)
    if (bl[i] >= bl[i + 1]) ans = 0;
  if (p[0].first != b[0]) ans = 0;
  if (!ans) {
    cout << ans;
    return 0;
  }
  int tp = 1, temp = bl[0], state = 0;
  for (int i = bl[0] + 1; ans && i < n; i++) {
    if (bl[tp] == i) {
      ans *= max(0, (bl[tp] - temp));
      ans %= mod;
      temp = i;
      tp++;
      if (tp == m) {
        while (++i < n)
          if (b[m - 1] > a[i]) ans = 0;
        break;
      }
    }
    if (b[tp - 1] > a[i]) ans = 0;
    if (b[tp] > a[i]) temp = max(temp, i);
  }
  cout << ans;
  return 0;
}
