#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int mod = 1e9 + 7;
int X[2048], Y[2048];
int dp[2048][2048][2][2];
int n, m;
int solve(int i, int j, bool p, bool q) {
  if (i == n || j == m) return (!p && !q);
  int &ans = dp[i][j][p][q];
  if (~ans) return ans;
  ans = 0;
  if (X[i] == Y[j]) ans = solve(i + 1, j + 1, 0, 0);
  if (!p && !q) ans = (ans + solve(i + 1, j + 1, 0, 0)) % mod;
  if (p) ans = (ans + solve(i, j + 1, 1, 0));
  if (q) ans = (ans + solve(i + 1, j, 0, 1)) % mod;
  if (!p && !q)
    ans =
        (ans + long long(solve(i + 1, j, 0, 1)) + solve(i, j + 1, 1, 0)) % mod;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> X[i];
  for (int i = 0; i < m; i++) cin >> Y[i];
  memset(dp, -1, sizeof dp);
  cout << solve(0, 0, 0, 0) << '\n';
  return 0;
}
