#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  int S[2001], T[2001];
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> T[i];
  }
  long long MOD = 1000000007;
  long** dp = new long*[2001];
  for (int i = 0; i < 2001; i++) {
    dp[i] = new long[2001];
  }
  for (int i = 0; i < N + 1; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < M + 1; i++) {
    dp[0][i] = 0;
  }
  for (int n = 1; n <= N; n++) {
    for (int m = 1; m <= M; m++) {
      dp[n][m] = dp[n][m - 1];
      long x = T[m - 1];
      for (int i = 0; i < n; i++) {
        if (S[i] == x) {
          dp[n][m] += 1 + dp[i][m - 1];
          dp[n][m] %= MOD;
        }
      }
    }
  }
  cout << dp[N][M] + 1 << endl;
  return 0;
}
