#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T>
inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const long long MOD = 1000000007;
int dp[2010][2010][2];
vector<long long> s, t;
map<int, vector<int> > vs;
int f(int a, int b, bool should_choose) {
  if (should_choose && b == t.size()) return 0;
  if (a == s.size() && b == t.size()) return 1;
  if (a == s.size()) return 1;
  if (dp[a][b][should_choose] != -1) return dp[a][b][should_choose];
  long long ans = 0;
  if (!should_choose) {
    ans += f(a + 1, b, false);
  }
  if (should_choose) {
    int nxt = *upper_bound(vs[s[a]].begin(), vs[s[a]].end(), b);
    assert(s[a] == t[b]);
    ans += f(a + 1, b + 1, false);
    ans += f(a, nxt, true);
  } else {
    int nxt = *lower_bound(vs[s[a]].begin(), vs[s[a]].end(), b);
    ans += f(a, nxt, true);
  }
  return dp[a][b][should_choose] = ans % 1000000007;
}
void solve(long long N, long long M, std::vector<long long> S,
           std::vector<long long> T) {
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < T.size(); i++) {
    vs[T[i]].push_back(i);
  }
  for (int i = 0; i <= 100000; i++) vs[i].push_back(M);
  s = S;
  t = T;
  cout << f(0, 0, 0) << endl;
}
int main() {
  long long N;
  scanf("%lld", &N);
  long long M;
  scanf("%lld", &M);
  std::vector<long long> S(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &S[i]);
  }
  std::vector<long long> T(M);
  for (int i = 0; i < M; i++) {
    scanf("%lld", &T[i]);
  }
  solve(N, M, std::move(S), std::move(T));
  return 0;
}
