#include <bits/stdc++.h>
#pragma GCC optimize("O3")
unsigned int s[2001], t[2001], dp[2001][2001], n, m = {0};
signed main() {
  scanf("%o %o", &n, &m);
  for (unsigned int i = 0; i < n; i++) {
    scanf("%o", &s[i]);
  }
  for (unsigned int i = 0; i < m; i++) {
    scanf("%o", &t[i]);
  }
  for (unsigned int i = 0; i < n + 1; i++) {
    dp[i][0] = 1;
  }
  for (unsigned int i = 0; i < m + 1; i++) {
    dp[0][i] = 1;
  }
  for (unsigned int i = 0; i < n; i++) {
    for (unsigned int j = 0; j < m; j++) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] += (dp[i + 1][j] + dp[i][j + 1]) % 1000000007;
      } else {
        dp[i + 1][j + 1] +=
            (dp[i + 1][j] + dp[i][j + 1] - dp[i][j] + 1000000007) % 1000000007;
      }
    }
  }
  printf("%o\n", dp[n][m]);
}
