#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int N, M;
  int *S, *T;
  long long max = 0, tmp;
  long long **dp, **sum;
  int imax = 0, jmax = 0;
  cin >> N >> M;
  S = new int[N];
  T = new int[M];
  sum = new long long *[N];
  dp = new long long *[N];
  for (int i = 0; i < N; i++) {
    sum[i] = new long long[M];
    dp[i] = new long long[M];
    cin >> S[i];
    sum[i][0] = 0;
  }
  for (int i = 0; i < M; i++) {
    cin >> T[i];
    sum[0][i] = 0;
  }
  max = 0;
  for (int i = 1; i < N; i++) {
    for (int j = 1; j < M; j++) {
      sum[i][j] =
          (sum[i - 1][j] + sum[i][j - 1] + (S[i] == T[j] ? 1 : 0)) % 1000000007;
      if (max < sum[i][j]) max = sum[i][j];
    }
  }
  cout << max << endl;
  return 0;
}
