#include <bits/stdc++.h>
using namespace std;
const int N = 2020;
const int mod = 1e9 + 7;
int n, m;
int a[N];
int b[N];
int dp[N][N];
int main() {
  cin >> n >> m;
  for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
       i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1)))
    cin >> a[i];
  for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
       i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1)))
    cin >> b[i];
  for (__typeof(n + 1) i = (0) - ((0) > (n + 1));
       i != (n + 1) - ((0) > (n + 1)); i += 1 - 2 * ((0) > (n + 1)))
    dp[i][0] = 1;
  for (__typeof(m + 1) i = (0) - ((0) > (m + 1));
       i != (m + 1) - ((0) > (m + 1)); i += 1 - 2 * ((0) > (m + 1)))
    dp[0][i] = 1;
  for (__typeof(n + 1) i = (1) - ((1) > (n + 1));
       i != (n + 1) - ((1) > (n + 1)); i += 1 - 2 * ((1) > (n + 1))) {
    for (__typeof(m + 1) j = (1) - ((1) > (m + 1));
         j != (m + 1) - ((1) > (m + 1)); j += 1 - 2 * ((1) > (m + 1))) {
      if (a[i] == b[j]) {
        dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
      } else
        dp[i][j] = (dp[i][j - 1] + dp[i - 1][j] - dp[i - 1][j - 1]);
      dp[i][j] = (dp[i][j] + mod) % mod;
    }
  }
  cout << dp[n][m];
}
