#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
long long n, m, s[2005], t[2005];
long long DP[2005][2005];
vector<long long> ind[100005];
long long fun(long long i, long long j = 2003) {
  if (i == n) return 1;
  if (DP[i][j] != -1) return DP[i][j];
  long long ret = fun(i + 1, j);
  long long nj = j;
  if (nj == 2003) nj = -1;
  nj = upper_bound(ind[s[i]].begin(), ind[s[i]].end(), nj) - ind[s[i]].begin();
  while (nj < ind[s[i]].size())
    ret = (ret + fun(i + 1, ind[s[i]][nj++])) % 1000000007;
  return DP[i][j] = ret;
}
void solve() {
  cin >> n >> m;
  for (long long i = 0; i < n; i++) cin >> s[i];
  for (long long i = 0; i < m; i++) cin >> t[i], ind[t[i]].push_back(i);
  bool sho = true, lo = true;
  for (long long i = 1; i < n; i++)
    if (s[i] != s[0]) {
      sho = false;
      break;
    };
  for (long long i = 1; i < m; i++)
    if (t[i] != t[0]) {
      lo = false;
      break;
    }
  if (sho && lo && s[0] == t[0]) {
    memset(DP, 0, sizeof DP);
    for (long long i = 0; i < 2003; i++) DP[i][0] = 1, DP[i][i] = 1;
    DP[0][0] = 1;
    for (long long i = 1; i < 2003; i++) {
      for (long long j = 1; j < i; j++)
        DP[i][j] = (DP[i - 1][j] + DP[i - 1][j - 1]) % 1000000007;
    }
    long long ret = 0, lim = min(n, m) + 1;
    for (long long i = 0; i < lim; i++)
      ret = (ret + (DP[n][i] * DP[m][i]) % 1000000007) % 1000000007;
    cout << ret << "\n";
    return;
  } else if (sho && lo) {
    cout << 1;
    return;
  } else {
    if (sho) {
      memset(DP, 0, sizeof DP);
      for (long long i = 0; i < 2003; i++) DP[i][0] = 1, DP[i][i] = 1;
      DP[0][0] = 1;
      for (long long i = 1; i < 2003; i++) {
        for (long long j = 1; j < i; j++)
          DP[i][j] = (DP[i - 1][j] + DP[i - 1][j - 1]) % 1000000007;
      }
      long long ret = 0, lim = min(n, (long long)ind[s[0]].size()) + 1;
      for (long long i = 0; i < lim; i++)
        ret = (ret + (DP[n][i] * DP[m][i]) % 1000000007) % 1000000007;
      cout << ret << "\n";
      return;
    } else if (lo) {
      for (long long i = 0; i < 100005; i++) ind[i].clear();
      for (long long i = 0; i < n; i++) ind[s[i]].push_back(i);
      memset(DP, 0, sizeof DP);
      for (long long i = 0; i < 2003; i++) DP[i][0] = 1, DP[i][i] = 1;
      DP[0][0] = 1;
      for (long long i = 1; i < 2003; i++) {
        for (long long j = 1; j < i; j++)
          DP[i][j] = (DP[i - 1][j] + DP[i - 1][j - 1]) % 1000000007;
      }
      long long ret = 0, lim = min(m, (long long)ind[t[0]].size()) + 1;
      for (long long i = 0; i < lim; i++)
        ret = (ret + (DP[n][i] * DP[m][i]) % 1000000007) % 1000000007;
      cout << ret << "\n";
      return;
    }
  }
  memset(DP, -1, sizeof DP);
  cout << "0";
  cout << fun(0) << "\n";
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  clock_t qwertyuiop = clock();
  long long t = 1;
  for (long long a = 0; a < t; a++) {
    solve();
  }
}
