#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(m);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  for (long long i = 0; i < m; ++i) cin >> b[i];
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  long long mn = 2e18, x = 0;
  long long res = 1;
  long long idx = 1;
  while (mn > b[0] && x < n) {
    mn = min(mn, a[x++]);
  }
  if (mn != b[0]) return cout << 0, 0;
  x--;
  while (true) {
    if (idx == m) {
      long long cur = 2e18;
      for (long long i = x; i < n; ++i) cur = min(cur, a[i]);
      if (cur != b[m - 1])
        return cout << 0, 0;
      else
        break;
    }
    long long y = x + 1, mid = -1;
    bool f = false;
    long long mn1 = a[y];
    while (b[idx] < mn1 && y < n) {
      if (!f && a[y] < a[x]) {
        f = true;
        mid = y;
      }
      mn1 = min(mn1, a[y]);
      ++y;
    }
    if (mn1 != b[idx]) return cout << 0, 0;
    if (f) --y;
    if (mid == -1) mid = y;
    res = (res * (mid - x)) % MOD;
    ++idx;
    x = y;
  }
  cout << res << '\n';
}
