#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n, m;
int a[2005], b[2005];
long long dp[2005][2005];
long long serc(int ia, int ib) {
  if (ia == -1 || ib == -1) return 1;
  if (dp[ia][ib] != -1) return dp[ia][ib];
  long long ret = 0;
  if (a[ia] == b[ib]) {
    ret += serc(ia, ib - 1) + serc(ia - 1, ib) - mod;
    if (ret >= mod) ret -= mod;
    if (ret < 0) ret += mod;
  } else {
    ret += serc(ia - 1, ib) + serc(ia, ib - 1) - serc(ia - 1, ib - 1) - mod;
    if (ret >= mod) ret -= mod;
    if (ret < 0) ret += mod;
  }
  if (ret >= mod) ret -= mod;
  return dp[ia][ib] = ret;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  memset(dp, -1, sizeof(dp));
  cout << serc(n - 1, m - 1) << '\n';
  return 0;
}
