#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<long long> s(n), t(m);
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> t[i];
  }
  long long def = 1;
  for (int i = 0; i < 9; i++) def *= 10;
  def += 7;
  vector<vector<long long>> dp(n, vector<long long>(m, 0));
  if (s[0] == t[0]) {
    dp[0][0] = 1;
  }
  for (int i = 0; i < n; i++) {
    if (i != 0) {
      dp[i][0] = dp[i - 1][0];
      if (s[i] == t[0]) {
        dp[i][0]++;
      }
    }
    map<int, vector<int>> ms;
    for (int j = n - 1; j >= 0; j--) {
      if (ms.count(s[j]) == 0) {
        vector<int> v;
        ms[s[j]] = v;
      }
      ms[s[j]].push_back(j);
    }
    for (int j = 1; j < m; j++) {
      dp[i][j] = dp[i][j - 1];
      if (ms.count(t[j]) == 0) continue;
      vector<int> tempv = ms[t[j]];
      long long sum = -1;
      while (tempv.size()) {
        int tempi = tempv.back();
        if (tempi > i) {
          dp[i][j] += -sum - 1;
          dp[i][j] %= def;
          tempv.push_back(-sum);
          break;
        }
        tempv.pop_back();
        if (tempi < 0) {
          sum = -tempi;
          if (tempv.size() == 0) {
            dp[i][j] += -sum - 1;
            dp[i][j] %= def;
            tempv.push_back(-sum);
            break;
          }
          continue;
        }
        if (tempi != 0) {
          sum -= dp[tempi - 1][j - 1];
        }
        sum--;
        sum %= def;
        if (tempv.size() == 0) {
          dp[i][j] += -sum - 1;
          dp[i][j] %= def;
          tempv.push_back(-sum);
          break;
        }
      }
    }
  }
  cout << dp[n - 1][m - 1] + 1 << endl;
  return 0;
}
