#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
vector<pair<int, int>> U[200000];
int A[200000], B[200000], ans[200000] = {1};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ((void)0);
  ((void)0);
  ((void)0);
  int N, M, j = 1;
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> A[i];
  for (int i = 0; i < N; i++) cin >> B[i];
  for (int i = N - 2; i >= 0; i--)
    if (A[i] > A[i + 1]) A[i] = A[i + 1];
  if (A[0] != B[0]) {
    cout << "0\n";
    return 0;
  }
  for (int i = 0; i < N; i++) {
    if (j < M && A[i] > B[j]) j++;
    if (j < M && A[i] == B[j]) {
      ans[j] += ans[j - 1];
      if (ans[j] >= MOD) ans[j] -= MOD;
    }
  }
  cout << ans[M - 1] << '\n';
  return 0;
}
