#include <bits/stdc++.h>
using namespace std;
const int Max = 2e6 + 10;
const int Mod = 998244353;
const double PI = 3.141592653589793238463;
int main() {
  (ios_base::sync_with_stdio(false), cin.tie(NULL));
  ;
  long long i, j, n, m, p, sum = 0, k, t, a, b, c, d, cnt = 0, q;
  double bb1, bb2, bb3, bb4, bb5, bb6, bb7, bb8;
  string lm1, lm2, lm3, lm5, lm6;
  float ha;
  string str;
  bool flag = true;
  cin >> n >> m;
  long long ara[n + 2], bra[n + 2];
  for (i = 0; i < n; i++) cin >> ara[i];
  for (i = 0; i < m; i++) cin >> bra[i];
  j = 0;
  vector<pair<long long, long long> > v;
  long long mi = 1e9;
  i = 0;
  long long last = 0;
  flag = true;
  while (i < n) {
    while (i < n && ara[i] >= bra[j]) {
      if (j + 1 < m && ara[i] == bra[j + 1]) break;
      i++;
    }
    if (last == i) {
      flag = false;
      break;
    }
    v.emplace_back(last, i - 1);
    last = i;
    j++;
  }
  p = i;
  i = 0;
  j = 0;
  while (i < n and j < m) {
    if (ara[i] == bra[j]) {
      i++;
      j++;
    } else
      i++;
  }
  if (flag && v.size() == m && p == n && j == m) {
    long long ans = 1;
    j = 0;
    for (i = 0; i < m; i++) {
      cnt = 1;
      long long l = v[i].first, r = v[i].second;
      long long cp = 0;
      while (l <= r) {
        if (ara[l] == bra[i]) cp++;
        l++;
      }
      long long kp = cp;
      l = v[i].first, r = v[i].second;
      if (i != 0) {
        while (l <= r && bra[i - 1] <= ara[l]) {
          if (bra[i] == ara[l]) {
            if (cp == 1)
              break;
            else
              cp--;
          }
          cnt++;
          l++;
        }
      }
      l = v[i].first, r = v[i].second;
      if (i + 1 < m) {
        cp = kp;
        while (r >= l && bra[i + 1] <= ara[r]) {
          if (bra[i] == ara[r]) {
            if (cp == 1)
              break;
            else
              cp--;
          }
          r--;
          cnt++;
        }
      }
      ans = (ans * cnt) % Mod;
      j++;
    }
    cout << ans % Mod << endl;
  } else
    cout << 0 << endl;
}
