#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
void amax(long long& a, long long b) {
  if (a < b) a = b;
}
signed main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> s(n), t(m);
  for (auto&& u : s) cin >> u;
  for (auto&& u : t) cin >> u;
  vector<vector<long long>> dp(n + 1, vector<long long>(m + 1, 0));
  for (long long i = 0; i < (n); i++)
    for (long long j = 0; j < (m); j++) {
      if (s[i] == t[j]) {
        amax(dp[i + 1][j + 1], dp[i][j] + 1);
        dp[i + 1][j + 1] %= mod;
      } else {
        amax(dp[i][j + 1], dp[i][j]);
        dp[i][j + 1] %= mod;
        amax(dp[i + 1][j], dp[i][j]);
        dp[i + 1][j] %= mod;
      }
    }
  cout << dp[n][m] << endl;
}
