#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % mod + mod) % mod) {}
  mint &operator+=(const mint &p) {
    if ((x += p.x) >= mod) x -= mod;
    return *this;
  }
  mint &operator-=(const mint &p) {
    if ((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
  mint &operator*=(const mint &p) {
    x = x * p.x % mod;
    return *this;
  }
  mint &operator/=(const mint &p) {
    *this *= p.inverse();
    return *this;
  }
  mint operator-() const { return mint(-x); }
  mint operator+(const mint &p) const { return mint(*this) += p; }
  mint operator-(const mint &p) const { return mint(*this) -= p; }
  mint operator*(const mint &p) const { return mint(*this) *= p; }
  mint operator/(const mint &p) const { return mint(*this) /= p; }
  bool operator==(const mint &p) const { return x == p.x; }
  bool operator!=(const mint &p) const { return x != p.x; }
  mint inverse() const {
    long long a = x, b = mod, u = 1, v = 0, t;
    while (b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return mint(u);
  }
  mint pow(long long n) const {
    mint ret(1), mul(x);
    while (n > 0) {
      if (n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
  friend ostream &operator<<(ostream &os, const mint &p) { return os << p.x; }
  friend istream &operator>>(istream &is, mint &a) {
    long long t;
    is >> t;
    a = mint(t);
    return (is);
  }
};
int main() {
  int N, M;
  cin >> N >> M;
  int S[N], T[N];
  for (int i = 0; i < (N); i++) cin >> S[i];
  for (int i = 0; i < (M); i++) cin >> T[i];
  mint dp[N + 1][M + 1], sum[N + 1][M + 1];
  dp[0][0] = 1;
  sum[0][0] = 1;
  for (int i = 1; i <= (N); i++) {
    dp[i][0] = 0;
    sum[i][0] = 1;
  }
  for (int j = 1; j <= (M); j++) {
    dp[0][j] = 0;
    sum[0][j] = 1;
  }
  for (int i = 1; i <= (N); i++) {
    for (int j = 1; j <= (M); j++) {
      if (S[i - 1] != T[j - 1]) {
        dp[i][j] = 0;
        sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
      } else {
        dp[i][j] = sum[i - 1][j - 1];
        sum[i][j] =
            sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + dp[i][j];
      }
    }
  }
  cout << (sum[N][M]) << endl;
  ;
  return 0;
}
