1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include<bits/stdc++.h>
using namespace std;

#define pb push_back
#define pii pair<int, int>
#define st first
#define nd second
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

const int MAXN = 10009;
const int MOD = 1e9+7;

int n, m;
int ans[MAXN*2];
int tab[2][MAXN];
vector<pii> dp[MAXN];
vector<pii> dp2[MAXN];

int Ceil(int a, int b) {
	return (a+b-1)/b;
}

void f(int L, bool same) {
	int cur = same?n:m;
	for(int i=3;i<=(same?n:m);i++) dp2[i] = dp[i];
	for(int i=1;i<L+same;i++) {
		int cnt = n-i+1;
		if(same) cnt = m-i+1;
		if(cnt==0) break;
		while(cur>=3&&sz(dp2[cur])==0) cur--;
		if(cur==2) {
			ans[2] += cnt;
			if(ans[2]>=MOD) ans[2]-=MOD;
			continue;
		} 
		pii x = dp2[cur].back();
		dp2[cur].pop_back();
		int newx = Ceil(x.st+x.nd+1, x.nd+2);
		if(newx>2) {
			dp2[newx].pb({x.st, x.nd+1});
		}
		while(cur>=3&&sz(dp2[cur])==0) cur--;
		ans[cur] += cnt;
		if(ans[cur]>=MOD) ans[cur]-=MOD;
	}
}

int32_t main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	cin >> n >> m;
	for(int i=0;i<n;i++) {
		cin >> tab[0][i];
	}
	for(int i=0;i<m;i++) {
		cin >> tab[1][i];
	}

	ans[2] = 1LL*n*m%MOD;
	for(int i=0;i<2;i++) {
		for(int j=0;j<(i?m:n)-1;j++) {
			for(int k=0;k<=(i?m:n);k++) {
				dp[k].clear();
			}
			int cur = 2;
			dp[cur].pb({cur, 0});
			f(2, 1-i);
			for(int k=j+2;k<(i?m:n);k++) {
				if((tab[i][k]<tab[i][k-1])==(tab[i][k-1]<tab[i][k-2])) {
					dp[cur].pop_back();
					cur++;
				} else {
					cur = 2;
				}
				dp[cur].pb({cur, 0});
				f(k-j+1, 1-i);
			}
		}
	}
	for(int i=1;i<=n+m;i++) {
		cout << ans[i] << " ";
	}
	cout << "\n";
}