#include <bits/stdc++.h>
const long long MOD = 1e9 + 7;
long long pow2(long long v, long long k) {
  long long res(1), tmp(v);
  while (k) {
    if (k & 1) res = (res * tmp) % MOD;
    tmp = (tmp * tmp) % MOD;
    k >>= 1;
  }
  return res;
}
signed main() {
  long long n, m;
  std::cin >> n >> m;
  std::vector<long long> s(n);
  for (long long i = 0; i < n; i++) std::cin >> s[i];
  std::vector<long long> t(m);
  for (long long i = 0; i < m; i++) std::cin >> t[i];
  std::vector<std::vector<long long> > dp(n + 1,
                                          std::vector<long long>(m + 1, 0));
  long long ans = 1;
  for (long long i = 0; i < n; i++)
    for (long long j = 0; j < m; j++) {
      dp[i + 1][j + 1] = std::max(dp[i + 1][j], dp[i][j + 1]);
      if (s[i] == t[j]) {
        ans = (ans + pow2(2, dp[i][j])) % MOD;
        dp[i + 1][j + 1] = dp[i][j] + 1;
      }
    }
  std::cout << (ans) << "\n";
  return 0;
}
