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
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r)for(int i=(l);i<=(r);++i)
#define REP(i,n)FOR(i,0,(n)-1)
#define ssize(x)int(x.size())
#ifdef DEBUG
auto operator<<(auto&o,auto x)->decltype(x.end(),o);
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<")";}
auto&operator<<(auto&o,tuple<auto,auto,auto,auto>t){return o<<"("<<get<0>(t)<<", "<<get<1>(t)<<", "<<get<2>(t)<<", "<<get<3>(t)<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

template<typename T>
void self_max(T& x, T y) {
	if (x < y)
		x = y;
}
template<typename T>
void self_min(T& x, T y) {
	if (x > y)
		x = y;
}
constexpr int mod = int(1e9) + 7;
int add(int a, int b) {
	a += b;
	return a >= mod ? a - mod : a;
}
void self_add(int& a, int b) {
	a += b;
	if (a >= mod)
		a -= mod;
}
int sub(int a, int b) {
	return add(a, mod - b);
}
int mul(int a, int b) {
	return int(a * LL(b) % mod);
}
int powi(int a, int b) {
	for(int ret = 1;; b /= 2) {
		if(b == 0)
			return ret;
		if(b & 1)
			ret = mul(ret, a);
		a = mul(a, a);
	}
}
int inv(int x) {
	return powi(x, mod - 2);
}
struct BinomCoeff {
	vector<int> fac, rev;
	BinomCoeff(int n) {
		fac = rev = vector(n + 1, 1);
		FOR(i, 1, n) fac[i] = mul(fac[i - 1], i);
		rev[n] = inv(fac[n]);
		for(int i = n; i > 0; --i)
			rev[i - 1] = mul(rev[i], i);
	}
	int operator()(int n, int k) {
		return mul(fac[n], mul(rev[n - k], rev[k]));
	}
};

int main() {
	cin.tie(0)->sync_with_stdio(0);

	int n, m;
	cin >> n >> m;
	vector<LL> ans(n + m + 1);

	vector<int> a(n), b(m);
	REP(i, n)
		cin >> a[i];
	REP(i, m)
		cin >> b[i];
	debug(n, m, a, b);

	FOR(len, 1, min(n, m)) {
		ans[2] += mul(n - len + 1, m - len + 1);
	}

	REP(iter, 2) {
		swap(n, m);
		swap(a, b);

		// prep(i, j, dir) - pierwsze r, że na [j-1,r] jest co najmniej wynik i gdzie elem i-1 ma daje dir, a przy dir=2 rozważamy [j,r]
		// all values in prep were decremented by 1
		vector prep(n + 3, vector (n, vector (3, n - 1)));
		REP(j, n) {
			REP(dir, 3) {
				int last_dir = dir;
				int last = 1 + (last_dir != 2), val = last;
				int i = 2;
				if (last == 2) {
					prep[i][j][dir] = j - 1;
					++i;
				}
				FOR(r, j + 1, n - 1) {
					int new_dir = a[r] < a[r - 1];
					if (new_dir != last_dir) {
						last_dir = new_dir;
						last = 1;
					}
					++last;
					if (last > val) {
						val = last;
						prep[i][j][dir] = r - 1;
						++i;
					}
				}
			}
		}
		debug(iter, a, b, prep);

		// dp(i, rb) - największe takie ra, że splot [la, ra] z [lb, rb] ma wynik <= i
		vector dp(n + 2, vector (m, n - 1));
		// dzięki tej kolejności rozważania lb, niedotknięte wartości dp będą ustawione na n - 1
		REP(la, n) {
			for (auto& v : dp)
				fill(v.begin(), v.end(), n - 1);
			for (int lb = m - 1; lb >= 0; --lb) {
				{
					int pos = prep[3][la][2];
					int rb = lb;
					while (rb < m and pos != n - 1) {
						int new_pos = prep[3][pos][b[rb] > a[pos]];
						if (not pos or (b[rb] < a[pos]) != (a[pos] < a[pos - 1])) {
							const int val = prep[3][pos + 1][a[pos + 1] < b[rb]];
							self_max(new_pos, val);
						}
						dp[2][rb] = pos = new_pos;
						++rb;
					}
				}
				FOR(rb, lb, m - 1) {
					ans[2] += max(0, (dp[2][rb] - la + 1) - (rb - lb + 1));
				}
				FOR(i, 3, n) {
					int pos = prep[i + 1][la][2];
					int rb = lb;
					while (rb < m and pos != n - 1) {
						int new_pos = prep[i + 1][pos][b[rb] > a[pos]];
						if (not pos or (b[rb] < a[pos]) != (a[pos] < a[pos - 1])) {
							const int val = prep[i + 1][pos + 1][a[pos + 1] < b[rb]];
							self_max(new_pos, val);
						}
						dp[i][rb] = pos = new_pos;
						ans[i] += dp[i][rb] - dp[i - 1][rb];
						++rb;
					}
					while (rb < m and dp[i - 1][rb] != n - 1) {
						ans[i] += dp[i][rb] - dp[i - 1][rb];
						++rb;
					}
					if (dp[i][lb] == n - 1)
						break;
				}
			}
		}
	}

	FOR(i, 1, n + m)
		cout << ans[i] % mod << (i == n + m ? '\n' : ' ');
}