#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  vector<int> S(N);
  vector<int> T(M);
  int MOD = 1e9 + 7;
  for (int i = 0; i < N; i++) cin >> S[i];
  for (int i = 0; i < M; i++) cin >> T[i];
  int dp[20001][20001];
  dp[0][0] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i] == T[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
      dp[i + 1][j + 1] %= MOD;
    }
  }
  cout << dp[N][M] << endl;
}
