#include <bits/stdc++.h>
const int inf = (1 << 30);
const int mod = 1000000007;
using ll = long long;
using namespace std;
const int bound = 2010;
ll dp[bound][bound];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(n);
  vector<int> t(m);
  for (auto &k : s) cin >> k;
  for (auto &k : t) cin >> k;
  for (int i = 0; i < bound; ++i) dp[i][0] = 1;
  for (int j = 0; j < bound; ++j) dp[0][j] = 1;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      dp[i + 1][j + 1] = (dp[i + 1][j] + dp[i][j + 1]) % mod;
      if (s[i] != t[j])
        dp[i + 1][j + 1] = (dp[i + 1][j + 1] - dp[i][j] + mod) % mod;
    }
  }
  for (int i = 0; i < 6; ++i) {
    for (int j = 0; j < 6; ++j) {
      cout << dp[i][j] << ' ';
    }
    cout << endl;
  }
  cout << dp[n][m] << endl;
}
