#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int main() {
  int n, m, x;
  scanf("%d%d", &n, &m);
  vector<pair<int, int> > v;
  for (int i = 0, x; i < n; i++) {
    cin >> x;
    while (!v.empty() && v.back().first >= x) v.pop_back();
    v.emplace_back(make_pair(x, i));
  }
  long long tmp = 0, ans = 1;
  for (int i = 0, x; i < m; i++) {
    scanf("%d", &x);
    if (i == 0 && v[tmp].first != x) ans = 0;
    while (tmp != v.size() && v[tmp].first != x) tmp++;
    if (tmp != v.size() && i) {
      ans *= (v[tmp].second - v[tmp - 1].second);
      tmp++;
      ans %= mod;
    } else if (tmp == v.size())
      ans = 0;
  }
  printf("%d\n", ans);
}
