#include <bits/stdc++.h>
using namespace std;
const long long int INF = (long long int)(1e18) + 1;
const int INF_INT = 2147483647 - 1e6;
const long long int mod = 1000000007ll;
struct mint {
  long long int x;
  mint(long long int x = 0) : x((x % mod + mod) % mod) {}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod - a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res += a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res -= a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res *= a;
  }
  mint pow(long long int t) const {
    if (!t) return 1;
    mint a = pow(t >> 1);
    a *= a;
    if (t & 1) a *= *this;
    return a;
  }
  mint inv() const { return pow(mod - 2); }
  mint& operator/=(const mint a) { return (*this) *= a.inv(); }
  mint operator/(const mint a) const {
    mint res(*this);
    return res /= a;
  }
};
int N, M;
int S[2000], T[2000];
long long int ans = -1;
mint dp0[2001][2001];
mint dp1[2001][2001];
void solve() {
  dp0[0][0] = 1;
  for (int i = 0; i <= N; i++) {
    for (int j = 0; j <= M; j++) {
      dp0[i + 1][j] += dp0[i][j];
      dp1[i][j] += dp0[i][j];
      dp1[i][j + 1] += dp1[i][j];
      if (S[i] == T[j]) {
        dp0[i + 1][j + 1] += dp1[i][j];
      }
    }
  }
  ans = dp1[N][M].x;
}
int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) cin >> S[i];
  for (int i = 0; i < M; i++) cin >> T[i];
  solve();
  cout << ans << endl;
  return 0;
}
