#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  cin >> N >> M;
  int s[N];
  int t[N];
  for (int i = 0; i < N; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < N; i++) {
    cin >> t[i];
  }
  long long dp[N + 1][M + 1];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < M + 1; j++) {
      dp[i][j] = 0;
    }
  }
  for (int i = 0; i < N + 1; i++) {
    dp[i][0] = 1;
  }
  for (int i = 0; i < M + 1; i++) {
    dp[0][i] = 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      dp[i + 1][j + 1] =
          (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] + 1000000007) % 1000000007;
      if (s[i] == t[i]) {
        dp[i + 1][j + 1] += dp[i][j];
        dp[i + 1][j + 1] %= 1000000007;
      }
    }
  }
  cout << dp[N][M] << endl;
}
