#include <bits/stdc++.h>
using namespace std;
static const int INTINF = (2147483647 >> 1);
static const long long LLINF = (9223372036854775807 >> 1);
static const int MAX = 1e5 + 1;
static const long long MOD = 1e9 + 7;
namespace Kunitaka {
template <typename TYPE, std::size_t SIZE>
std::size_t array_length(const TYPE (&array)[SIZE]) {
  return SIZE;
}
}  // namespace Kunitaka
using namespace Kunitaka;
namespace Printer {
void br() { cout << endl; }
void loop_cnt(int i) { cout << "i = " << i << endl; }
void loop_cnt(int i, int j) {
  cout << "i = " << i << ", "
       << "j = " << j << endl;
}
template <class T>
void print(T x) {
  cout << x << endl;
}
template <class T, class S>
void print(T x, S y) {
  cout << x << "," << y << endl;
}
template <class T, class S, class U>
void print(T x, S y, U z) {
  cout << x << "," << y << "," << z << endl;
}
template <class T, class S>
void print(pair<T, S> p) {
  cout << p.first << ", " << p.second << endl;
}
template <typename TYPE, std::size_t SIZE>
void print(const TYPE (&array)[SIZE]) {
  int lim = array_length(array);
  for (int i = 0; i < lim; i++) {
    if (i) cout << " ";
    cout << array[i];
  }
  cout << endl;
}
template <class S>
void print(vector<S> v) {
  for (int i = 0; i < v.size(); i++) {
    if (i) cout << " ";
    cout << v[i];
  }
  cout << endl;
}
template <class S>
void print(vector<vector<S> > vv) {
  for (int i = 0; i < vv.size(); i++) {
    for (int j = 0; j < vv[i].size(); j++) {
      if (j) cout << " ";
      cout << vv[i][j];
    }
    cout << endl;
  }
}
void yesno(bool x) {
  if (x)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
};  // namespace Printer
using namespace Printer;
long long dp[10000][10000];
int main(int argc, const char* argv[]) {
  cout << fixed << setprecision(10);
  long long H, W;
  cin >> H >> W;
  vector<long long> A(H), B(W);
  for (int i = 0; i < (H); ++i) cin >> A[i];
  for (int j = 0; j < (W); ++j) cin >> B[j];
  memset(dp, 0, sizeof(dp));
  dp[0][0] = 1;
  for (int i = 0; i < (H + 1); ++i) dp[i][0] = 1;
  for (int j = 0; j < (W + 1); ++j) dp[0][j] = 1;
  for (int i = 0; i < (H); ++i) {
    for (int j = 0; j < (W); ++j) {
      int ni = i + 1;
      int nj = j + 1;
      if (A[i] == B[j]) {
        dp[ni][nj] = dp[ni - 1][nj - 1] % MOD;
      }
      dp[ni][nj] = (dp[ni][nj] % MOD + dp[ni - 1][nj] % MOD +
                    dp[ni][nj - 1] % MOD - dp[ni - 1][nj - 1]);
      while (dp[ni][nj] < 0) dp[ni][nj] += MOD;
    }
  }
  print(dp[H][W]);
  return 0;
}
