#include<iostream>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<map>
#include<iomanip>
#include<sstream>
#include<vector>
#include<stack>
#include<math.h>
#include<queue>
using namespace std;
long long int res_map[2005][2005] = {};

int main() {
	const long long int mod = 1000000007;
	map<long long int, long long int>mp;
	//cout << fixed << setprecision(10);
	long double big, small, wa;
	long long int n, m,k,x, res = 0, cou = 1, mi = 999999999, ma = -9999999999,tmp[2005];
	long long int max_id, min_id, L[200000], w, h, y, a[200001], sum[200001] = {};
	long long int s[2005], t[2005];
	cin >>n>>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 < m; i++) {
		for (int j = 0; j < n; j++) {
			if (s[j] == t[i]) {
				res_map[i][j] = 1 + sum[j];
				res_map[i][j] %= mod;
			}
		}
		tmp[0] = 0;
		for (int j = 1; j <= n; j++) {
			
			tmp[j] = res_map[i][j-1]+tmp[j-1];

			
		}
		for (int j = 1; j <= n; j++) {
			
			sum[j] = sum[j] + tmp[j];
			sum[i]\ = mod;

		}
		cout << endl;
	}
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < n; j++) {
			res += res_map[i][j];
			cout << res_map[i][j]<<" ";
		}
		cout << endl;
	}
	cout << (res+1)%mod;
}
