#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T get() {
  T x;
  cin >> x;
  return x;
}
int main() {
  int n = get<int>();
  int m = get<int>();
  vector<int> s(n);
  for (int i = 0; i < n; i++) s[i] = get<int>();
  vector<int> t(n);
  for (int i = 0; i < n; i++) t[i] = get<int>();
  vector<vector<int>> sum(n + 1);
  for (int i = 0; i < n + 1; i++) sum[i].resize(m + 1);
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i == 0 || j == 0) {
        sum[i][j] = 1;
      } else {
        long tmp = (long)sum[i - 1][j] + sum[i][j - 1] + 1000000007 -
                   sum[i - 1][j - 1] +
                   (s[i - 1] == t[j - 1] ? sum[i - 1][j - 1] : 0);
        sum[i][j] = tmp % 1000000007;
      }
    }
  }
  cout << sum[n][m] << endl;
  return 0;
}
