#include <bits/stdc++.h>
#pragma warning(disable : 4996)
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("-ffloat-store")
using namespace std;
const double PI = acos(-1.0);
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int n;
  cin >> n;
  long long int m;
  cin >> m;
  vector<long long int> s(n, 0);
  for (long long int i = 0; i < n; i += 1) {
    cin >> s[i];
  };
  vector<long long int> t(n, 0);
  for (long long int i = 0; i < n; i += 1) {
    cin >> t[i];
  };
  long long int n1 = n;
  long long int n2 = m;
  long long int dp[n1 + 1][n2 + 1];
  for (long long int i = 0; i < n1 + 1; i += 1)
    for (long long int j = 0; j < n2 + 1; j += 1) dp[i][j] = 0;
  for (long long int i = 1; i <= n1; i++) {
    for (long long int j = 1; j <= n2; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = (1 + dp[i][j - 1] + dp[i - 1][j]) % 1000000007;
      else
        dp[i][j] =
            (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]) % 1000000007;
    }
  }
  cout << (dp[n1][n2] + 1) % 1000000007 << endl;
  return 0;
}
