#include <bits/stdc++.h>
using namespace std;
const int N = 1002, M = 1e9 + 7;
void Max(int &x, int y) {
  if (y > x) x = y;
}
void Min(int &x, int y) {
  if (y < x) x = y;
}
void inc(int &x, int y) {
  x += y;
  if (x >= M) x -= M;
}
void dec(int &x, int y) {
  x -= y;
  if (x < 0) x += M;
}
int pw(int x, int y) {
  int z = 1;
  for (; y; y >>= 1, x = 1ll * x * x % M)
    if (y & 1) z = 1ll * z * x % M;
  return z;
}
void read(int *a, int n) {
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
}
int n, m, i, j, f[N][N], a[N], b[N];
signed main() {
  scanf("%d%d", &n, &m);
  read(a, n), read(b, m);
  for (i = 0; i <= n; i++) f[i][0] = 1;
  for (i = 0; i <= m; i++) f[0][i] = 1;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++)
      inc(f[i][j], 1ll * f[i - 1][j] + f[i][j - 1] - f[i - 1][j - 1] +
                       (a[i] == b[j]) * f[i - 1][j - 1]);
  printf("%d", f[n][m]);
}
