#include <bits/stdc++.h>
using namespace std;
using lint = long long;
const int inf = 1000000007;
const int MOD = 1000000007;
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> s(n + 1, 0);
  vector<int> t(m + 1, 0);
  vector<vector<lint>> dp(n + 1, vector<lint>(m + 1, 1));
  for (int i = 1; i <= n; ++i) cin >> s[i];
  for (int i = 1; i <= m; ++i) cin >> t[i];
  dp[0][0] = 1;
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      dp[i][j] = dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      dp[i][j] %= MOD;
      if (s[i] == t[j]) {
        dp[i][j] += dp[i - 1][j - 1];
        dp[i][j] %= MOD;
      }
    }
  }
  cout << dp[n][m] << "\n";
  for (int i = 1; i <= n * 0; ++i) {
    for (int j = 1; j <= m; ++j) {
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
