#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
long long mult(long long a, long long b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long add(long long a, long long b) {
  return ((a % mod) + (b % mod)) % mod;
}
long long sub(long long a, long long b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
int solve() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n), b(m);
  map<int, int> lastocc;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
    lastocc[b[i]] = -1;
  }
  bool iszero = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] < b[0]) {
      iszero = 1;
    }
    lastocc[a[i]] = i;
  }
  for (int i = 0; i < m; ++i) {
    if (lastocc[b[i]] == -1) {
      iszero = 1;
    }
  }
  if (lastocc[b[m - 1]] != -1) {
    for (int i = lastocc[b[m - 1]] + 1; i < n; ++i) {
      if (a[i] < b[m - 1]) {
        iszero = 1;
      }
    }
  }
  if (m > n || iszero) {
    cout << 0;
    return 0;
  }
  vector<int> smaller(n);
  stack<pair<int, int>> st;
  for (int i = n - 1; i >= 0; --i) {
    if (st.empty()) {
      st.push({a[i], i});
    }
    bool yes = 0;
    while (!st.empty() && a[i] < st.top().first) {
      smaller[st.top().second] = i;
      st.pop();
      yes = 1;
    }
    if (!st.empty() && st.top().first == a[i]) {
      continue;
    }
    if (yes) {
      st.push({a[i], i});
    } else {
      st.push({a[i], i});
    }
  }
  vector<int> lft(m, -1), rgt(m, -1);
  lft[m - 1] = smaller[lastocc[b[m - 1]]] + 1;
  rgt[m - 1] = n - 1;
  int i = lft[m - 1] - 1, j = m - 2;
  while (i >= 0 && j >= 0) {
    while (i >= 0 && a[i] > b[j]) {
      --i;
    }
    if (a[i] < b[j]) {
      cout << 0;
      return 0;
    }
    rgt[j] = lastocc[b[j + 1]] - 1;
    lft[j] = smaller[i] + 1;
    i = smaller[i];
    --j;
  }
  lft[0] = 0;
  vector<int> hash(n);
  for (int i = 0; i < m; ++i) {
    for (int j = lft[i]; j <= rgt[i]; ++j) {
      ++hash[j];
    }
  }
  long long ans = 1, cnt = 0;
  for (int i = 0; i < n; ++i) {
    if (hash[i] > 1) {
      cnt++;
    } else {
      if (cnt != 0) {
        ans = mult(ans, cnt + 1);
        cnt = 0;
      }
    }
  }
  cout << ans;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
