#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
int n, m;
int a[((int)2e3 + 5)];
int b[((int)2e3 + 5)];
int dp[((int)2e3 + 5)][((int)2e3 + 5)];
map<int, int> mpa[100 * ((int)2e3 + 5)];
map<int, int> mpb[100 * ((int)2e3 + 5)];
long long rec(int x, int y) {
  if (x == 0 and y == 0)
    return (a[0] == b[0] ? 1 : 0);
  else if (x == 0 or y == 0) {
    if (x == 0) return mpb[y][a[0]];
    return mpa[x][b[0]];
  }
  if (dp[x][y] != -1) return dp[x][y];
  long long p1 = ((a[x] == b[y]) ? rec(x - 1, y - 1) + 1 : 0);
  long long p2 = rec(x - 1, y);
  long long p3 = rec(x, y - 1);
  long long p4 = rec(x - 1, y - 1);
  return dp[x][y] = (p1 + p2 + p3 - p4) % ((int)1e9 + 7);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j) dp[i][j] = -1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if (i) {
      mpa[i] = mpa[i - 1];
      mpa[i][a[i]]++;
    } else
      mpa[i][a[i]]++;
  }
  for (int i = 0; i < m; ++i) {
    cin >> b[i];
    if (i) {
      mpb[i] = mpb[i - 1];
      mpb[i][b[i]]++;
    } else
      mpb[i][b[i]]++;
  }
  rec(n - 1, m - 1);
  cout << (dp[n - 1][m - 1] + 1) % ((int)1e9 + 7) << "\n";
}
