#include <bits/stdc++.h>
using i64 = int64_t;
using namespace std;
const int MAX_N = 2000;
const int64_t MOD = 1e9 + 7;
i64 DP[MAX_N + 10][MAX_N + 10];
vector<int> S, T;
vector<set<int>> K;
int main() {
  int N, M;
  cin >> N >> M;
  S = vector<int>(N), T = vector<int>(M);
  K = vector<set<int>>(N + 1, set<int>());
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < M; i++) {
    cin >> T[i];
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (S[i] == T[j]) {
        K[i].insert(j);
      }
    }
  }
  for (int i = N; i >= 0; i--) {
    auto itor = K[i].end();
    if (K[i].size()) itor--;
    i64 sum = 0;
    for (int j = M; j >= 0; j--) {
      if (i == N) {
        DP[i][j] = 1;
      } else {
        if (K[i].size() && (*itor) == j) {
          sum += DP[i + 1][j + 1];
          sum %= MOD;
          itor--;
        }
        DP[i][j] = sum + DP[i + 1][j];
        DP[i][j] %= MOD;
      }
    }
  }
  cout << DP[0][0] << endl;
  return 0;
}
