#include <bits/stdc++.h>
using namespace std;
template <typename T, typename U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}
template <typename T, typename U, typename V>
ostream &operator<<(ostream &os, const tuple<T, U, V> &t) {
  os << "(" << get<0>(t) << "," << get<1>(t) << "," << get<2>(t) << ")";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto it = v.begin(); it != v.end(); ++it) {
    if (it != v.begin()) os << " ";
    os << *it;
  }
  return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &mp) {
  for (auto x : mp) os << "(" << x.first << "," << x.second << ")" << endl;
  return os;
}
template <typename T, long long SIZE>
long long array_length(const T (&)[SIZE]) {
  return SIZE;
}
template <typename T, long long N>
void PRINTF(const T (&a)[N], long long s = N, long long t = -1, bool f = true) {
  if (t == -1) {
    for (long long i = 0; i < s; i++) {
      if (i) cout << " ";
      cout << a[i];
    }
  } else
    for (long long i = s; i < t; i++) {
      if (i != s) cout << " ";
      cout << a[i];
    }
  if (f) cout << "\n";
}
template <typename T, long long N1, long long N2>
void PRINTF(const T (&a)[N1][N2], long long h = N1, long long w = N2) {
  for (long long i = 0; i < h; i++) {
    for (long long j = 0; j < w; j++) {
      cout << a[i][j] << " \n"[j == w - 1];
    }
  }
}
string substr(const string &str, long long S1, long long S2 = -1) {
  if (S2 == -1) return str.substr(S1);
  return str.substr(S1, S2 - S1);
}
const long long inf = 1e9 + 7;
const long long INF = 1e18 + 7;
const long long mod = inf;
long long s[3000], t[3000], q[3000][3000], p[3000][3000];
signed main() {
  long long n, m;
  cin >> n >> m;
  for (long long i = 1; i < n + 1; i++) cin >> s[i];
  for (long long i = 1; i < m + 1; i++) cin >> t[i];
  for (long long i = 0; i < m + 1; i++) q[0][i] = 1;
  for (long long i = 0; i < n + 1; i++) q[i][0] = 1;
  for (long long i = 1; i < n + 1; i++)
    for (long long j = 1; j < m + 1; j++) {
      if (s[i] == t[j]) {
        p[i][j] = q[i - 1][j - 1];
      }
      if (q[i][j - 1] + q[i - 1][j] < q[i - 1][j - 1] + p[i][j])
        while (1)
          ;
      q[i][j] = q[i][j - 1] + q[i - 1][j] - q[i - 1][j - 1] + p[i][j];
      q[i][j] %= mod;
    }
  cout << q[n][m] << endl;
  return 0;
}
