#include <bits/stdc++.h>
using namespace std;
long long read() {
  long long res = 0;
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) res = res * 10 + c - '0', c = getchar();
  return res;
}
const long long maxn = 2e3 + 3;
const long long mod = 1e9 + 7;
long long n, m, a[maxn], b[maxn];
long long dp[maxn][maxn];
int main() {
  n = read();
  m = read();
  dp[0][0] = 1;
  for (long long i = 1; i <= n; i++) a[i] = read();
  for (long long i = 1; i <= m; i++) b[i] = read();
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= m; j++) {
      (dp[i][j] += dp[i - 1][j - 1]) %= mod;
      (dp[i][j] += dp[i][j - 1]) %= mod;
      (dp[i][j] += dp[i + 1][j]) %= mod;
      if (a[i] == b[j]) (dp[i][j] += dp[i - 1][j - 1]) %= mod;
    }
  }
  printf("%lld\n", dp[n][m]);
  return 0;
}
