#include <bits/stdc++.h>
using namespace std;
const int M = 998244353;
int n, m;
int main() {
  while (scanf("%d %d", &n, &m) == 2) {
    vector<int> a(n), b(m);
    for (auto &x : a) scanf("%d", &x);
    for (auto &x : b) scanf("%d", &x);
    unordered_map<int, int> pos;
    pos.reserve(n);
    for (int i = 0; i < n; i++) pos[a[i]] = i;
    int res = 1;
    for (auto &x : b)
      if (!pos.count(x)) res = 0;
    for (int i = 0; i < m - 1 && res; i++)
      if (pos[b[i + 1]] < pos[b[i]]) res = 0;
    for (int i = 0; i < m && res; i++)
      for (int j = (i == 0 ? 0 : pos[b[i]]);
           j < (i == m - 1 ? n : pos[b[i + 1]]) && res; j++)
        res *= a[j] >= b[i] ? 1 : 0;
    for (int i = 1; i < m && res; i++) {
      int j = pos[b[i]];
      while (j >= 0 && a[j] >= b[i]) j--;
      res = 1ll * res * (pos[b[i]] - j) % M;
    }
    printf("%d\n", res);
  }
  return 0;
}
