#include <bits/stdc++.h>
using namespace std;
int main(void) {
  long long n, m;
  cin >> n >> m;
  vector<long long> s = vector<long long>(n);
  vector<long long> t = vector<long long>(m);
  vector<vector<long long>> sIndex = vector<vector<long long>>(n);
  vector<vector<long long>> tIndex = vector<vector<long long>>(m);
  for (int i = 0; i < n; i++) cin >> s[i];
  for (int i = 0; i < m; i++) cin >> t[i];
  vector<vector<long long>> sAns =
      vector<vector<long long>>(n, vector<long long>(n, 0));
  vector<vector<long long>> tAns =
      vector<vector<long long>>(m, vector<long long>(m, 0));
  if (n < m) {
    for (int i = n - 1; i >= 0; i--) {
      auto iter = find(t.begin(), t.end(), s[i]);
      while (iter != t.end()) {
        long long value = distance(t.begin(), iter);
        sIndex[i].push_back(value);
        sAns[i][value]++;
        iter = find(next(iter + 1), t.end(), s[i]);
        if (i < n - 1) {
          for (int j = 0; j < sIndex[i + 1].size(); j++) {
            long long ind = sIndex[i + 1][j];
            if (value < ind) {
              for (int k = j; k < sIndex[i + 1].size(); k++) {
                sAns[i][value] += 2 * sAns[i + 1][ind];
                sAns[i][value] %= 1000000007;
              }
            }
          }
        } else {
          for (int j = 0; j < sIndex[i].size(); j++) sAns[i][sIndex[i][j]] = 1;
        }
      }
    }
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      ans += sAns[0][i];
      ans %= 1000000007;
    }
    cout << ans << endl;
  } else {
    for (int i = m - 1; i >= 0; i--) {
      auto iter = find(s.begin(), s.end(), t[i]);
      while (iter != s.end()) {
        long long value = distance(s.begin(), iter);
        tIndex[i].push_back(value);
        tAns[i][value]++;
        iter = find(next(iter + 1), s.end(), t[i]);
        if (i < m - 1) {
          for (int j = 0; j < tIndex[i + 1].size(); j++) {
            long long ind = tIndex[i + 1][j];
            if (value < ind) {
              for (int k = j; k < tIndex[i + 1].size(); k++) {
                tAns[i][value] += 2 * tAns[i + 1][ind];
                tAns[i][value] %= 1000000007;
              }
            }
          }
        } else {
          for (int j = 0; j < tIndex[i].size(); j++) tAns[i][sIndex[i][j]] = 1;
        }
      }
    }
    long long ans = 0;
    for (int i = 0; i < m; i++) {
      ans += tAns[0][i];
      ans %= 1000000007;
    }
    cout << ans << endl;
  }
}
