#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
void d_err() { cerr << endl; }
template <typename H, typename... T>
void d_err(H h, T... t) {
  cerr << h << " ";
  d_err(t...);
}
template <typename T>
void print(T x) {
  cout << x << endl;
}
long long INF = 1e16;
int inf = 1e9;
long long MOD = 1e9 + 7;
struct mint {
  long long x;
  mint(long long x = 0) : x((x % MOD + MOD) % MOD) {}
  mint operator-() const { return mint(-x); }
  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 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;
  }
};
ostream& operator<<(ostream& os, const mint a) {
  os << a.x;
  return os;
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(20);
  int N, M;
  cin >> N >> M;
  vector<int> S(N), T(M);
  for (int i = (0); i < (N); ++i) cin >> S[i];
  for (int j = (0); j < (M); ++j) cin >> T[j];
  reverse((S).begin(), (S).end());
  reverse((T).begin(), (T).end());
  vector<vector<mint>> dp(N, vector<mint>(M, 0));
  dp[0][0] = 1;
  for (int i = (0); i < (N); ++i) {
    for (int j = (0); j < (M); ++j) {
      if (i > 0) dp[i][j] += dp[i - 1][j];
      if (j > 0) dp[i][j] += dp[i][j - 1];
      if (i > 0 && j > 0) dp[i][j] -= 1;
      if (S[i] == T[j]) {
        if (i > 0 && j > 0)
          dp[i][j] += dp[i - 1][j - 1] * 2;
        else
          dp[i][j] += 1;
      }
    }
    83;
  }
  print(dp[N - 1][M - 1]);
}
