#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
using namespace std;
void fast() {
  ios::sync_with_stdio(0);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
void my_file_in_out() {
  freopen("inp.txt", "r", stdin);
  freopen("out.txt", "w", stdout);
}
inline void OK(string t = "") { cerr << "OK " << t << '\n'; }
inline long double time() { return clock() / (long double)CLOCKS_PER_SEC; }
inline void stop(string t = "PROCESS FINISHED") {
  cerr << t << '\n';
  exit(0);
}
const long long M = 998244353;
const int MAXN = 2e5 + 10;
long long bp(long long c, long long st) {
  if (!st) return 1;
  if (st & 1) return (c * bp(c, st - 1)) % M;
  return bp((c * c) % M, st >> 1);
}
namespace VECTOR_MAGIC {
template <typename T>
istream &operator>>(istream &inp, vector<T> &a) {
  for (auto &elem : a) inp >> elem;
  return inp;
}
template <typename T>
ostream &operator<<(ostream &out, vector<T> &a) {
  for (auto &elem : a) out << elem << ' ';
  out << endl;
  return out;
}
template <typename T>
ostream &operator<<(ostream &out, vector<vector<T>> &a) {
  for (auto &elem : a) out << elem;
  return out;
}
template <typename T>
void operator++(vector<T> &a) {
  for (auto &el : a) el++;
}
template <typename T>
void operator++(vector<T> &a, T) {
  for (auto &el : a) el++;
}
template <typename T>
void operator--(vector<T> &a) {
  for (auto &el : a) el--;
}
template <typename T>
void operator--(vector<T> &a, T) {
  for (auto &el : a) el--;
}
template <typename T>
void operator+=(vector<T> &a, T X) {
  for (auto &el : a) el += X;
}
}  // namespace VECTOR_MAGIC
using namespace VECTOR_MAGIC;
mt19937 rnd(time(0));
struct sparse_rmq {
  vector<vector<int>> dp;
  vector<int> lg;
  void calc(vector<int> &a) {
    int d = ((int)a.size());
    lg.resize(d + 5, 0);
    for (int i = 2; i < ((int)lg.size()); ++i) lg[i] = lg[i / 2] + 1;
    dp.resize(22, vector<int>(d));
    for (int i = 0; i < d; ++i) dp[0][i] = a[i];
    for (int i = 1; i < 22; ++i)
      for (int j = 0; j < d - (1 << i) + 1; ++j)
        dp[i][j] = min(dp[i - 1][j], dp[i - 1][j + (1 << (i - 1))]);
  }
  int rmq(int l, int r) {
    int d = lg[r - l + 1];
    return min(dp[d][l], dp[d][r - (1 << d) + 1]);
  }
};
void solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  cin >> a >> b;
  if (m == 1) {
    int x = *min_element(a.begin(), a.end());
    if (x == b[0])
      cout << 1;
    else
      cout << 0;
    return;
  }
  a.push_back(int(1e9 + 7));
  vector<int> pos(m, -1);
  sparse_rmq sub;
  sub.calc(a);
  int j = 0;
  for (int i = 0; i <= n && j < m; i++) {
    if (sub.rmq(i, n) > b[j]) {
      pos[j] = i - 1;
      j++;
    }
  }
  for (int i = 0; i < m; i++) {
    if (pos[i] == -1) {
      cout << 0;
      return;
    }
  }
  vector<int> lef(m, 0);
  for (int i = 1; i < m; i++) {
    int l = 0;
    int r = pos[i];
    while (r - l > 1) {
      int m = (l + r) / 2;
      if (sub.rmq(m, pos[i]) < b[i])
        l = m;
      else
        r = m;
    }
    lef[i] = r;
  }
  for (int i = 0; i < m; i++) {
    if (i == m - 1) {
      int l = lef[i];
      int r = n - 1;
      if (sub.rmq(l, r) != b[i]) {
        cout << 0;
        return;
      }
      continue;
    }
    int l = lef[i];
    int r = pos[i + 1] - 1;
    if (sub.rmq(l, r) != b[i]) {
      cout << 0;
      return;
    }
  }
  long long ans = 1;
  for (int i = 1; i < m; i++) {
    long long d = pos[i] - lef[i] + 1;
    ans *= d;
    ans %= M;
  }
  cout << ans;
}
signed main() {
  fast();
  int t = 1;
  while (t--) solve();
  return 0;
}
