#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long int;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;

//struct edge {int from, to, cost;};

constexpr double EPS = 1e-10;
//constexpr double PI  = acos(-1.0);
//constexpr int INF = INT_MAX;
constexpr int MOD = 1'000'000'007;
//inline void modAdd(LL &l, LL &r) {l = (l + r) % MOD;}

template<class T> inline T sqr(T x) {return x*x;}

void solve() {
    LL n,m;
    cin >> n >> m;
    VLL s(n), t(m);
    REP(i,0,n) cin >> s[i];
    REP(i,0,m) cin >> t[i];
    /*
    if (s[i] == t[j]) dp[i][j] = max(dp[i][j], dp[i-1][j-1] * 2);
     */
    VVI sru(n+1, VI(100000+1));
    VVI tru(m+1, VI(100000+1));
    REP(i,0,n) {
        REP(j,1,100000+1) sru[i+1][j] = sru[i][j];
        sru[i+1][s[i]]++; 
    }
    REP(i,0,m) {
        REP(j,1,100000+1) tru[i+1][j] = tru[i][j];
        tru[i+1][t[i]]++; 
    }

    VI sprev(n,-1), tprev(m,-1), snext(n,n), tnext(m,m);
    REP(i,0,n) {
        REP(j,i+1,n) if (s[j] == s[i]) {
            snext[i] = j;
            break;
        }
        RREP(j,0,i) if (s[j] == s[i]) {
            sprev[i] = j;
            break;
        }
    }
    REP(i,0,m) {
        REP(j,i+1,m) if (t[j] == t[i]) {
            tnext[i] = j;
            break;
        }
        RREP(j,0,i) if (t[j] == t[i]) {
            tprev[i] = j;
            break;
        }
    }



    VVLL dp(n+1, VLL(m+1));
    REP(i,0,n+1) dp[i][0] = 1;
    REP(i,0,m+1) dp[0][i] = 1;
    REP(i,0,n) REP(j,0,m) {
        /*
        if (s[i] == t[j]) dp[i+1][j+1] = max(dp[i+1][j+1], (dp[i][j] * 2 + sru[i][s[i] + tru[j][t[j]]]) % MOD);
        else dp[i+1][j+1] = max(dp[i+1][j+1], (dp[i+1][j] + dp[i][j+1] - dp[i][j]) % MOD)
        */
        if (s[i] == t[j]) dp[i+1][j+1] = max(dp[i+1][j+1], (dp[i][j] * 2 + (tprev[i] >= 0 ? dp[i][tprev[i]] : 0) + (sprev[j] >= 0 ? dp[sprev[j]][j] : 0)) % MOD);
        else dp[i+1][j+1] = max(dp[i+1][j+1], (dp[i][j] + (tprev[i] >= 0 ? dp[i][tprev[i]] : 0) + (sprev[j] >= 0 ? dp[sprev[j]][j] : 0)) % MOD);
    }
    REP(i,0,n+1) {
        REP(j,0,m+1) cout << dp[i][j] << " ";
        cout << endl;
    } 
    cout << dp[n][m] << endl;
}

int main() {
    solve();
    
    return 0;
}