#include <bits/stdc++.h>
using namespace std;
template <class T>
using vc = vector<T>;
template <class T>
using vvc = vc<vc<T>>;
template <class T>
void mkuni(vector<T>& v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}
long long rand_int(long long l, long long r) {
  static mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
  return uniform_int_distribution<long long>(l, r)(gen);
}
template <class T>
void print(T x, int suc = 1) {
  cout << x;
  if (suc == 1)
    cout << '\n';
  else
    cout << ' ';
}
template <class T>
void print(const vector<T>& v, int suc = 1) {
  for (int i = 0; i < v.size(); i++)
    print(v[i], i == (int)(v.size()) - 1 ? suc : 2);
}
const int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int j = 0; j < m; j++) cin >> b[j];
  vector<int> fi(m), las(m), lasap(m, -1);
  int p = m - 1;
  bool suc = 1;
  las[p] = n - 1;
  for (int i = n - 1; i >= 0; i--) {
    while (a[i] < b[p]) {
      fi[p] = i + 1;
      p--;
      if (p < 0) {
        suc = 0;
        break;
      }
      las[p] = i;
    }
    if (!suc) break;
    if (a[i] == b[p] && lasap[p] == -1) lasap[p] = i;
  }
  if (p != 0) suc = 0;
  for (int i = 0; i < m; i++) {
    if (fi[i] > las[i] || lasap[i] == -1) suc = 0;
  }
  long long ans = 1;
  if (!suc)
    ans = 0;
  else {
    for (int i = 0; i < m - 1; i++)
      ans = ans * (lasap[i + 1] - fi[i + 1] + 1) % mod;
  }
  print(ans);
}
