#include <bits/stdc++.h>
using namespace std;
const int oo = 2e5 + 7;
const int mod = 998244353;
int n, m;
long long a[oo];
long long b[oo];
int rangel[oo];
int ranger[oo];
int aidsl[oo];
int aidsr[oo];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  int tcounter = 0;
  for (int i = 0; i < n; i++)
    if (a[i] == b[tcounter]) aidsl[tcounter++] = i;
  if (tcounter != m) {
    cout << 0 << endl;
    return 0;
  }
  tcounter = m - 1;
  for (int i = n - 1; i >= 0; i--)
    if (a[i] == b[tcounter]) aidsr[tcounter--] = i;
  if (tcounter != -1) {
    cout << 0 << endl;
    return 0;
  }
  {
    stack<int> st;
    for (int i = 0; i < n; i++) {
      while (st.size() != 0 && a[st.top()] >= a[i]) st.pop();
      rangel[i] = st.size() == 0 ? -1 : st.top();
      st.push(i);
    }
    stack<int> stt;
    for (int i = n - 1; i >= 0; i--) {
      while (stt.size() != 0 && a[stt.top()] >= a[i]) stt.pop();
      ranger[i] = stt.size() == 0 ? INT_MAX : stt.top();
      stt.push(i);
    }
  }
  if (rangel[aidsl[0]] != -1 || ranger[aidsr[m - 1]] != INT_MAX) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 1;
  for (int i = 0; i < m - 1; i++) {
    long long mult = min(ranger[aidsl[i]] - 1, aidsr[i + 1] - 1) -
                     max(rangel[aidsr[i + 1]] + 1, aidsl[i] + 1) + 2;
    if (mult <= 0) {
      ans = 0;
      break;
    }
    ans *= (mult % mod);
    ans %= mod;
  }
  cout << ans << endl;
}
