#include <bits/stdc++.h>

#include <atcoder/all>

// #LIB#

using namespace std;
using namespace atcoder;
using ll = long long;
#define double long double
#define rep(i, n) for (long long i = 0; i < (n); ++i)
#define repr(i, a, b) for (auto i = (a); i < (b); ++i)
#define itr(x, c) for (auto&& x : (c))
#define updatemax(t, v) ((t) = std::max((t), (v)))
#define updatemin(t, v) ((t) = std::min((t), (v)))
#ifdef ONLINE_JUDGE
#define endl _endl
const char _endl = (cin.tie(0), cout.tie(0), ios::sync_with_stdio(0), cout.precision(16), '\n');
#endif

using mint = static_modint<1000000007>;

struct T {
  mint val;
  int size;
};
T st_op(T a, T b) {
  return T{a.val + b.val, a.size + b.size};
}
T st_e() {
  return T{0, 0};
}
T st_map(mint x, T a) {
  return T{a.val + x * a.size, a.size};
}
mint st_comp(mint x, mint y) {
  return x + y;
}
mint st_id() {
  return 0;
}

int main() {
  ll n, m;
  cin >> n >> m;
  vector<ll> s(n);
  rep(i, n) cin >> s[i];
  vector<ll> t(m);
  rep(i, m) cin >> t[i];
  lazy_segtree<T, st_op, st_e, mint, st_map, st_comp, st_id> st(vector<T>(m + 1, T{1, 1}));
  rep(i, n) {
    auto v = s[i];
    for (int j = m - 1; j >= 0; --j) {
      if (t[j] != v) continue;
      st.apply(j + 1, m + 1, st.get(j).val);
    }
  }
  cout << st.get(m).val.val() << endl;
}
