#include <bits/stdc++.h>
using namespace std;
template <typename T>
std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}
const int MOD = 1e9 + 7;
const long long LINF = 1001002003004005006ll;
const int INF = 1001001001;
inline int add(int x, int y) { return (x + y) % MOD; }
inline int sub(int x, int y) { return (x + MOD - y) % MOD; }
inline int mul(int x, int y) { return (long long)x * y % MOD; }
long long dp[2010][2010] = {};
long long acc[2010][2010] = {};
signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N, M;
  cin >> N >> M;
  vector<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 = 1; i <= N; i++) {
    for (int j = 1; j <= M; j++) {
      if (S[i - 1] != T[j - 1]) {
        dp[i][j] = 0;
      } else {
        dp[i][j] = (acc[i - 1][j - 1] + 1) % MOD;
      }
      acc[i][j] = sub(add(add(acc[i - 1][j], acc[i][j - 1]), dp[i][j]),
                      acc[i - 1][j - 1]);
    }
  }
  for (int i = (0); i < (N + 1); ++i) {
    for (int j = (0); j < (M + 1); ++j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  for (int i = (0); i < (N + 1); ++i) {
    for (int j = (0); j < (M + 1); ++j) {
      cout << acc[i][j] << " ";
    }
    cout << endl;
  }
  cout << acc[N][M] + 1 << endl;
}
