#include <bits/stdc++.h>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
template <ostream& out = cout, class... T>
void prints(T&&...) {}
template <ostream& out = cout, class... T>
void printw(T&&...) {}
template <ostream& out = cout, class... T>
void printwe(T&&...) {}
template <ostream& out = cout, class... T>
void printb(T&&...) {}
template <ostream& out = cout, class... T>
void printbe(T&&...) {}
template <ostream& out = cout, class... T>
void printd(T&&...) {}
template <ostream& out = cout, class... T>
void printde(T&&...) {}
template <ostream& out = cout, class... T>
void printdu(T&&...){};
template <class S, class T>
istream& operator>>(istream& in, pair<S, T>& p) {
  return in >> p.first >> p.second;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
  for (auto& e : v) {
    in >> e;
  }
  return in;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  vector<int> ss(n);
  cin >> ss;
  vector<int> ts(m);
  cin >> ts;
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));
  for (int i = 0; i <= n; i++) {
    dp[i][0] = 1;
  }
  for (int i = 0; i <= m; 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];
      if (dp[i + 1][j + 1] >= M) {
        dp[i + 1][j + 1] -= M;
      }
      if (ss[i] == ts[j]) {
        dp[i + 1][j + 1] += dp[i][j];
        if (dp[i + 1][j + 1] >= M) {
          dp[i + 1][j + 1] -= M;
        }
      }
    }
  }
  printde(dp);
  cout << dp[n][m] << '\n';
}
