#include <bits/stdc++.h>
using namespace std;
int main() {
  long long N, M;
  cin >> N >> M;
  vector<long long> S(N), T(M);
  for (long long i = 0; i < N; i++) cin >> S[i];
  for (long long i = 0; i < M; i++) cin >> T[i];
  vector<vector<long long> > DP(N, vector<long long>(M, 0));
  for (long long s = 0; s < N; s++) {
    for (long long t = 0; t < M; t++) {
      if (s == 0 && t == 0) {
        DP[s][t] = (S[s] == T[t]) + 1;
      } else if (s == 0) {
        DP[s][t] = ((S[s] == T[t]) + DP[s][t - 1]) % 1000000007;
      } else if (t == 0) {
        DP[s][t] = ((S[s] == T[t]) + DP[s - 1][t]) % 1000000007;
      } else {
        long long a = ((S[s] == T[t]) * (DP[s - 1][t - 1])) % 1000000007;
        long long b =
            ((DP[s - 1][t] - DP[s - 1][t - 1]) % 1000000007 + DP[s][t - 1]) %
            1000000007;
        DP[s][t] = (a + b) % 1000000007;
      }
    }
  }
  assert(DP[N - 1][M - 1] > 0);
  cout << DP[N - 1][M - 1];
}
