#include <bits/stdc++.h>
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
using namespace std;
const int mod = 1e9 + 7;
long long dp[2010][2010], a[2010], b[2010];
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 1; i < n + 1; i++) cin >> a[i];
  for (int i = 1; i < n + 1; i++) cin >> b[i];
  for (int i = 0; i < n + 1; i++) dp[0][i] = dp[i][0] = 1;
  for (int i = 1; i < n + 1; i++)
    for (int j = 1; j < m + 1; j++) {
      dp[i][j] = (dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1]) % mod;
      if (a[i] == b[j]) dp[i][j] = (dp[i - 1][j - 1] + dp[i][j]) % mod;
    }
  cout << dp[n][m] << endl;
  return 0;
}
