#include <bits/stdc++.h>
using namespace std;
const int INF = 1e+9;
const long long INFLL = 1e+17;
const long long MOD = 1e+9 + 7;
int N, M;
int S[2010], T[2010];
long long dp[2010][2010];
map<int, long long> m;
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> S[i];
  for (int i = 0; i < M; i++) cin >> T[i];
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < N + 1; i++) dp[i][0] = 1;
  for (int j = 0; j < M + 1; j++) dp[0][j] = 1;
  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (S[i - 1] == T[j - 1]) {
        dp[i][j] = (dp[i][j] + dp[i - 1][j - 1] * 2 % MOD) % MOD;
        dp[i][j] = (dp[i][j] + m[T[j - 1]] * 2) % MOD;
      } else {
        dp[i][j] = dp[i - 1][j];
        dp[i][j] = (dp[i][j] + m[T[j - 1]]) % MOD;
      }
    }
    m[S[i - 1]]++;
  }
  cout << dp[N][M] << "\n";
  return 0;
}
