#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
using ll = long long;
ll mod = 998244353;
ll a[maxn], b[maxn];
map<ll, vector<int>> mp;
int main(int argc, char const *argv[]) {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld", &a[i]);
    mp[a[i]].push_back(i);
  }
  int last = -1;
  for (int i = 1; i <= m; ++i) {
    scanf("%lld", &b[i]);
    if (mp.find(b[i]) == mp.end()) {
      cout << "0" << '\n';
      return 0;
    }
    if (last != -1 && mp[b[i]].back() < last) {
      cout << "0" << '\n';
      return 0;
    }
    last = mp[b[i]].back();
  }
  for (int i = 1; i <= mp[b[1]].back(); ++i) {
    if (a[i] < b[1]) {
      cout << "0" << '\n';
      return 0;
    }
  }
  for (int i = mp[b[m]].back() + 1; i <= n; ++i) {
    if (a[i] < b[m]) {
      cout << "0" << '\n';
      return 0;
    }
  }
  ll ans = 1;
  for (int i = 1; i < m; ++i) {
    int x = b[i], y = b[i + 1], st = mp[x].back();
    for (int j = mp[x].back(); j < mp[y].back(); ++j) {
      if (a[j] < x) {
        cout << "0" << '\n';
        return 0;
      }
      if (a[j] < y) {
        st = j;
      }
    }
    ans = (ans * (mp[b[i + 1]].back() - st + mod) % mod) % mod;
  }
  cout << ans << '\n';
  return 0;
}
