#include <bits/stdc++.h>
using namespace std;
int INF = 2147483647;
long long LINF = 9223372036854775807;
long long MOD = 1000000007;
int main() {
  int m, n;
  cin >> n >> m;
  long long dp[2020][2020] = {};
  long long s[n], t[m];
  for (int i = 0; i < n; ++i) cin >> s[i];
  ;
  for (int i = 0; i < m; ++i) cin >> t[i];
  ;
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (s[i] == t[j]) {
        dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] + 1;
        dp[i + 1][j + 1] %= MOD;
      } else {
        dp[i + 1][j + 1] = dp[i + 1][j] + dp[i][j + 1] - dp[i][j];
        dp[i + 1][j + 1] %= MOD;
      }
    }
  }
  for (int i = 0; i < n + 1; ++i) {
    for (int j = 0; j < m + 1; ++j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  if (dp[n][m] < 0) dp[n][m] += MOD;
  cout << dp[n][m] + 1 << endl;
  return 0;
}
