#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 1e9 + 7;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll n, m;
  cin >> n >> m;
  vector<ll> s(n + 1), t(m + 1);
  unordered_map<ll, vector<ll>> spos, tpos;
  for (ll i = 1; i <= n; ++i) {
    cin >> s[i];
    spos[s[i]].push_back(i);
  }
  for (ll i = 1; i <= m; ++i) {
    cin >> t[i];
    tpos[t[i]].push_back(i);
  }
  vector<vector<ll>> cnt(n + 1, vector<ll>(m + 1));
  for (ll i = 0; i <= n; ++i) cnt[i][0] = 1;
  for (ll j = 0; j <= m; ++j) cnt[0][j] = 1;
  for (ll i = 1; i <= n; ++i) {
    for (ll j = 1; j <= m; ++j) {
      cnt[i][j] = cnt[i][j - 1];
      if (!spos.count(t[j])) continue;
      for (ll k : spos[t[j]]) {
        if (k > i) break;
        cnt[i][j] = (cnt[i][j] + cnt[k - 1][j - 1]) % MOD;
      }
    }
  }
  cout << cnt[n][m] % MOD;
  return 0;
}
