#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
long long n, m, s[3005], t[3005];
long long DP[3005][3005];
vector<long long> ind[100005];
long long fun(long long i, long long j = 3003) {
  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 == 3003) 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 = 0; i < n; i++)
    if (s[i] != s[0]) sho = false;
  for (long long i = 0; i < m; i++)
    if (t[i] != t[0]) sho = false;
  if (sho) exit(10);
  memset(DP, -1, sizeof DP);
  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();
  }
}
