#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9;
long long dp[2020][2020];
int main() {
  long long n, m;
  cin >> n >> m;
  vector<long long> s(n), t(m);
  for (long long i = 0; i < long long(n); i++) cin >> s[i];
  for (long long i = 0; i < long long(m); i++) cin >> t[i];
  dp[0][0] = 1;
  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      if (i - 1 >= 0 && j - 1 >= 0 && s[i - 1] != t[j - 1]) {
        dp[i][j] -= dp[i - 1][j - 1];
      }
      if (i - 1 >= 0) {
        dp[i][j] += dp[i - 1][j];
      }
      if (j - 1 >= 0) {
        dp[i][j] += dp[i][j - 1];
      }
      dp[i][j] = (mod + dp[i][j]) % mod;
    }
  }
  cout << dp[n][m] << endl;
}
