#include <bits/stdc++.h>
using namespace std;
const long long maxn = 2e3 + 10;
const long long mod = 1e9 + 7;
long long ans = 0;
long long n, m;
long long lcs[maxn][maxn];
long long a[maxn];
long long b[maxn];
void cal() {
  lcs[1][1] = a[1] == b[1] ? 2 : 1;
  for (long long i = 2; i <= n; ++i) {
    lcs[1][i] = lcs[1][i - 1];
    if (b[1] == a[i]) lcs[1][i] = lcs[1][i - 1] + 1;
  }
  for (long long i = 2; i <= m; ++i) {
    lcs[i][1] = lcs[i - 1][1];
    if (a[1] == b[i]) lcs[i][1] = lcs[i - 1][1] + 1;
  }
  for (long long i = 2; i <= m; ++i) {
    for (long long j = 2; j <= n; ++j) {
      if (a[j] == b[i]) {
        lcs[i][j] = (lcs[i - 1][j] + lcs[i][j - 1]) % mod;
      } else {
        if (lcs[i - 1][j] == lcs[i][j - 1]) {
          lcs[i][j] = (2 * lcs[i - 1][j] - lcs[i - 1][j - 1]) % mod;
        } else
          lcs[i][j] = (lcs[i][j - 1] + lcs[i - 1][j] - lcs[i - 1][j - 1]) % mod;
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (long long i = 1; i <= n; ++i) cin >> a[i];
  for (long long i = 1; i <= m; ++i) cin >> b[i];
  lcs[0][0] = 1;
  cal();
  cout << (lcs[m][n]) % mod << endl;
}
