#include <bits/stdc++.h>
using namespace std;
using llong = long long;
using uint = unsigned int;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const llong mod = 998244353;
void No() {
  cout << 0;
  exit(0);
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  for (int i = 0; i < n; ++i) cin >> a[i];
  for (int i = 0; i < m; ++i) cin >> b[i];
  if (m > n) No();
  vector<int> l(m);
  for (int i = n - 1, j = m; i >= 0; --i)
    while (j - 1 >= 0 && a[i] < b[j - 1]) l[--j] = i + 1;
  if (l[0]) No();
  l.push_back(n);
  llong ans = 1;
  for (int i = 0; i < m; ++i) {
    int cnt = l[i + 1] - l[i];
    if (!cnt) No();
    for (int j = l[i + 1] - 1; j >= l[i]; --j)
      if (a[j] == b[i])
        break;
      else
        --cnt;
    if (!cnt) No();
    if (i) ans *= cnt;
    ans %= mod;
  }
  cout << ans;
}
