#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long MOD = 1000000007LL;
vector<long long> S;
vector<long long> T;
vector<vector<long long>> dp;
map<int, vector<int>> sm;
map<int, vector<int>> tm;
long long solve(const int si, const int ti, const long long N,
                const long long M) {
  if (si == 0 || ti == 0) {
    return 1;
  }
  if (dp[si][ti] >= 0) {
    return dp[si][ti];
  }
  long long ans;
  if (S[si] == T[ti]) {
    ans = solve(si - 1, ti - 1, N, M) * 2 % MOD;
  } else {
    ans = solve(si - 1, ti - 1, N, M);
  }
  {
    int i = si;
    vector<int> v = sm[T[ti]];
    auto p = lower_bound(v.begin(), v.end(), i);
    if (p != v.begin()) {
      p--;
      while (p >= v.begin()) {
        ans = (ans + solve(*p - 1, ti - 1, N, M)) % MOD;
        p--;
      }
    }
  }
  {
    int i = ti;
    vector<int> v = tm[S[si]];
    auto p = lower_bound(v.begin(), v.end(), i);
    if (p != v.begin()) {
      p--;
      while (p >= v.begin()) {
        ans = (ans + solve(si - 1, *p - 1, N, M)) % MOD;
        p--;
      }
    }
  }
  dp[si][ti] = ans;
  return dp[si][ti];
}
int main(void) {
  long long N, M;
  cin >> N >> M;
  S.resize(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    cin >> S[i];
    sm[S[i]].push_back(i);
  }
  T.resize(M + 1, 0);
  for (int i = 1; i <= M; i++) {
    cin >> T[i];
    tm[T[i]].push_back(i);
  }
  dp.resize(N + 1, vector<long long>(M + 1, -1));
  cout << solve(N, M, N, M) << endl;
  return 0;
}
