#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e3 + 2;
const int mod = 1e9 + 7;
long long n, m, a[maxn], b[maxn], sum[maxn][maxn], dp[maxn][maxn];
long long add(long long a, long long b) {
  return ((a % mod) + (b % mod)) % mod;
}
long long sub(long long a, long long b) {
  return ((a % mod) - (b % mod) + mod) % mod;
}
int main(int argc, char const *argv[]) {
  while (cin >> n >> m) {
    for (int i = (0), __i = (n); i < __i; i += (1)) cin >> a[i];
    for (int i = (0), __i = (m); i < __i; i += (1)) cin >> b[i];
    memset(dp, 0, sizeof(dp));
    memset(sum, 0, sizeof(sum));
    for (int i = (0), __i = (n); i < __i; i += (1)) {
      for (int j = (0), __j = (m); j < __j; j += (1)) {
        if (a[i] == b[j]) {
          dp[i][j] = 1;
          if (i >= 1 && j >= 1) dp[i][j] = add(dp[i][j], sum[i - 1][j - 1]);
        }
        sum[i][j] = dp[i][j];
        if (i >= 1) sum[i][j] = add(sum[i][j], sum[i - 1][j]);
        if (j >= 1) sum[i][j] = add(sum[i][j], sum[i][j - 1]);
        if (i >= 1 && j >= 1) sum[i][j] = sub(sum[i][j], sum[i - 1][j - 1]);
      }
    }
    cout << sum[n - 1][m - 1] + (a[n - 1] == b[m - 1]) << endl;
  }
  return 0;
}
