#include <bits/stdc++.h>
long mod(long n, long k) { return n % k >= 0 ? n % k : n % k + k; }
long buf[((2 * (1L << 11L) - 1) * (2 * (1L << 11L) - 1))];
void st_init() {
  for (long k = 0; k < ((2 * (1L << 11L) - 1) * (2 * (1L << 11L) - 1)); k++) {
    buf[k] = (0L);
  }
}
void st_update(long i, long j, long v) {
  const long cur_i0 = (1L << 11L) - 1 + i, cur_j0 = (1L << 11L) - 1 + j,
             cur0 = cur_j0 + (2 * (1L << 11L) - 1) * cur_i0;
  buf[cur0] = v;
  for (long cur_j = cur_j0; cur_j > 0; cur_j = (cur_j - 1) / 2) {
    const long par_j = (cur_j - 1) / 2;
    const long chl_i = cur_i0, chl_j = 2 * par_j + 1,
               chl = chl_j + (2 * (1L << 11L) - 1) * chl_i;
    const long chr_i = cur_i0, chr_j = 2 * par_j + 2,
               chr = chr_j + (2 * (1L << 11L) - 1) * chr_i;
    const long par = par_j + (2 * (1L << 11L) - 1) * cur_i0;
    buf[par] = mod(buf[chl] + buf[chr], (1000000007L));
  }
  for (long cur_j = cur_j0;; cur_j = (cur_j - 1) / 2) {
    const long par_j = cur_j;
    for (long cur_i = cur_i0, par_i = (cur_i - 1) / 2; cur_i > 0;
         cur_i = par_i, par_i = (cur_i - 1) / 2) {
      const long cur = cur_j + (2 * (1L << 11L) - 1) * cur_i,
                 par = par_j + (2 * (1L << 11L) - 1) * par_i;
      const long chl_i = 2 * par_i + 1, chl_j = par_j,
                 chl = chl_j + (2 * (1L << 11L) - 1) * chl_i;
      const long chr_i = 2 * par_i + 2, chr_j = par_j,
                 chr = chr_j + (2 * (1L << 11L) - 1) * chr_i;
      buf[par] = mod(buf[chl] + buf[chr], (1000000007L));
    }
    if (cur_j == 0) {
      break;
    }
  }
}
long st_query_j(long ib, long ie, long jb, long je, long cur_i, long cur_j,
                long cur_ib, long cur_ie, long cur_jb, long cur_je) {
  if (je <= cur_jb || cur_je <= jb) {
    return (0L);
  } else if (jb <= cur_jb && cur_je <= je) {
    const long cur = cur_j + (2 * (1L << 11L) - 1) * cur_i;
    return buf[cur];
  } else {
    const long cur_jm = (cur_jb + cur_je) / 2;
    const long chl_j = 2 * cur_j + 1, chr_j = 2 * cur_j + 2;
    return mod(st_query_j(ib, ie, jb, je, cur_i, chl_j, cur_ib, cur_ie, cur_jb,
                          cur_jm) +
                   st_query_j(ib, ie, jb, je, cur_i, chr_j, cur_ib, cur_ie,
                              cur_jm, cur_je),
               (1000000007L));
  }
}
long st_query_i(long ib, long ie, long jb, long je, long cur_i, long cur_ib,
                long cur_ie) {
  if (ie <= cur_ib || cur_ie <= ib) {
    return (0L);
  } else if (ib <= cur_ib && cur_ie <= ie) {
    return st_query_j(ib, ie, jb, je, cur_i, 0, cur_ib, cur_ie, 0, (1L << 11L));
  } else {
    const long cur_im = (cur_ib + cur_ie) / 2;
    const long chl_i = 2 * cur_i + 1, chr_i = 2 * cur_i + 2;
    return st_query_i(ib, ie, jb, je, chl_i, cur_ib, cur_im) +
           st_query_i(ib, ie, jb, je, chr_i, cur_im, cur_ie);
  }
}
long st_query(long ib, long ie, long jb, long je) {
  return st_query_i(ib, ie, jb, je, 0, 0, (1L << 11L));
}
void st_print() {
  long cur_i = 0;
  for (long w = (1L << 11L); w > 0; w /= 2) {
    for (long cur_ib = 0; cur_ib < (1L << 11L); cur_ib += w) {
      printf("cur_i = %ld\n", cur_i);
      printf("[cur_ib, cur_ie) = [%ld, %ld): ", cur_ib, cur_ib + w);
      for (long cur_j = 0; cur_j < 2 * (1L << 11L) - 1; cur_j++) {
        const long cur = cur_j + (2 * (1L << 11L) - 1) * cur_i;
        printf("%ld ", buf[cur]);
      }
      printf("\n");
      cur_i++;
    }
  }
}
int main(int argc, char *argv[]) {
  long N, M, as[(2000L)], bs[(2000L)];
  scanf("%ld %ld", &N, &M);
  for (long i = 0; i < N; i++) {
    scanf("%ld", &as[i]);
  }
  for (long j = 0; j < M; j++) {
    scanf("%ld", &bs[j]);
  }
  st_init();
  st_update(N, M, 1);
  for (long i = N - 1; i >= 0; i--) {
    for (long j = M - 1; j >= 0; j--) {
      if (as[i] == bs[j]) {
        st_update(i, j, st_query(i + 1, N + 1, j + 1, M + 1));
      } else {
        st_update(i, j, 0);
      }
    }
  }
  printf("%ld\n", st_query(0, N + 1, 0, M + 1));
  return 0;
}
