#include <bits/stdc++.h>
using namespace std;
long long n, m, T, x, y, x2, y2, a, b, sol = 1;
char c;
string s;
bool t;
vector<int> V;
vector<int> V2;
int p, p2, mn = INT_MAX;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x;
    V.push_back(x);
  }
  for (int i = 0; i < m; i++) {
    cin >> x;
    V2.push_back(x);
  }
  p = n - 1;
  for (int i = 0; i < m - 1; i++) {
    x = V2[m - i - 1];
    while (p >= 0 && V[p] > x) {
      p--;
    }
    if (p < 0) {
      cout << 0;
      return 0;
    }
    if (V[p] < x) sol = 0;
    p2 = p;
    while (p >= 0 && V[p2] >= x) {
      p2--;
    }
    if (p2 < 0) {
      cout << 0;
      return 0;
    }
    sol *= p - p2;
    sol %= 998244353;
    p = p2;
  }
  x = V2[0];
  for (int i = 0; i <= p; i++) {
    mn = min(V[i], mn);
  }
  if (mn != x) sol = 0;
  cout << sol;
}
