#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long mod = pow(10, 9) + 7;
  long long N, M;
  cin >> N >> M;
  vector<long long> S(N), T(M);
  vector<vector<long long>> s_num(pow(10, 5) + 1), t_num(pow(10, 5) + 1);
  for (long long i = 0; i < N; ++i) {
    cin >> S.at(i);
    s_num.at(S.at(i)).push_back(i);
  }
  for (long long i = 0; i < M; ++i) {
    cin >> T.at(i);
    t_num.at(T.at(i)).push_back(i);
  }
  vector<vector<long long>> dp(N + 1, vector<long long>(M + 1, 0));
  dp.at(0).at(0) = 1;
  for (long long i = 0; i <= N; ++i) {
    for (long long j = 0; j <= M; ++j) {
      long long temp = 0;
      if (j != M) {
        for (long long k = 0; k < s_num.at(T.at(j)).size(); ++k) {
          if (s_num.at(T.at(j)).at(k) >= i) {
            break;
          }
          temp = (temp + dp.at(s_num.at(T.at(j)).at(k)).at(j)) % mod;
        }
        dp.at(i).at(j + 1) =
            max(dp.at(i).at(j + 1), (dp.at(i).at(j) + temp) % mod);
      }
      temp = 0;
      if (i != N) {
        for (long long k = 0; k < t_num.at(S.at(i)).size(); ++k) {
          if (t_num.at(S.at(i)).at(k) >= j) {
            break;
          }
          temp = (temp + dp.at(i).at(t_num.at(S.at(i)).at(k))) % mod;
        }
        dp.at(i + 1).at(j) =
            max(dp.at(i + 1).at(j), (dp.at(i).at(j) + temp) % mod);
      }
    }
  }
  long long ans = dp.at(N).at(M);
  cout << ans << "\n";
}
