#include <bits/stdc++.h>
using namespace std;
const int MOD = 1000000007;
const int INF = (1 << 28);
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
const int Dx[8] = {0, 1, 1, 1, 0, -1, -1, -1},
          Dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int n, m;
int s[10000];
int t[10000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < m; i++) cin >> t[i];
  vector<vector<int> > s1;
  vector<vector<int> > t1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (s[i] == t[j] && i >= j) {
        s1[i].push_back(j);
      }
      if (s[i] == t[j] && i <= j) {
        t1[j].push_back(i);
      }
    }
  }
  long long ans = 1;
  for (int i = 0; i < n; i++) {
    for (int k = 0; k < s1.size(); k++) {
      for (int j = s1[i][k]; j < m; j++) {
        if (t1[j].size() != 0) ans *= t1[j].size();
        ans %= MOD;
      }
    }
  }
  cout << ans << "\n";
}
