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
171
172
173
174
175
176
177
178
179
180
181
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   vi         = vector<int>;
using   pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

constexpr int MOD = int(1e9) + 7;

constexpr ll modPow(ll a, ll e, ll m) {
	ll t = 1 % m;
	while (e) {
		if (e % 2) t = t*a % m;
		e /= 2; a = a*a % m;
	}
	return t;
}

struct Zp {
	ll x;
	constexpr Zp() : x(0) {}
	constexpr Zp(ll a) : x(a%MOD) { if (x < 0) x += MOD; }
	#define OP(c,d) constexpr Zp& operator c##=(Zp r) { x = x d; return *this; } constexpr Zp operator c(Zp r) const { Zp t = *this; return t c##= r; }
	OP(+, +r.x - MOD*(x+r.x >= MOD));
	OP(-, -r.x + MOD*(x-r.x < 0));
	OP(*, *r.x % MOD);
	OP(/, *r.inv().x % MOD);
	constexpr Zp operator-() const { return Zp()-*this; }
	constexpr Zp inv() const { return pow(MOD-2); }
	constexpr Zp pow(ll e) const { return modPow(x,e,MOD); }
	void print() { cerr << x; }
};

using Poly = array<Zp, 3>;
constexpr Zp INV2 = Zp(2).inv();

Zp eval(const Poly& p, Zp x) { return (p[2]*x + p[1]) * x + p[0]; }
Poly shift(const Poly& p, Zp t) { return { eval(p, t), p[2]*t*2 + p[1], p[2] }; }
Poly operator*(Poly l, Zp r) { return { l[0]*r, l[1]*r, l[2]*r }; }

Poly& operator+=(Poly& l, const Poly& r) {
	rep(i, 0, sz(l)) l[i] += r[i];
	return l;
}

Poly& operator-=(Poly& l, const Poly& r) {
	rep(i, 0, sz(l)) l[i] -= r[i];
	return l;
}

struct Tail {
	int begin = 0, pos = 0, absPos = 0, mx = 0;
	Poly poly = {};
	Zp get(Zp x) const { return eval(poly, x); }
};

vector<Zp> ans; // ans[x] = #{ (A', B') : f(A', B') > x }

void solve(const vector<pair<int, bool>>& blocks, int m, int x) {
	Poly base = { -m-1, INV2*3+m, -INV2 };

	auto push = [&](Tail& t, int i) {
		auto [len, active] = blocks[i];
		if (active) {
			int sh = (len+x-1) / x - 1;
			t.mx += sh;
			t.poly = shift(t.poly, sh);
			for (int j = 0; j*x < len; j++) {
				t.poly += shift(base, j) * min(x, len-j*x);
			}
		} else {
			t.poly += base * len;
		}
	};

	auto pop = [&](Tail& t) {
		int from = t.absPos, oldMx = t.mx;
		if (blocks[t.begin].y && t.pos > x) {
			int oldPos = t.pos;
			t.pos = (t.pos-1) / x * x;
			t.absPos += oldPos - t.pos;
			t.mx--;
		} else {
			t.absPos += t.pos;
			t.pos = blocks[++t.begin].x;
		}
		t.poly -= shift(base, oldMx) * (t.absPos - from);
	};

	Tail near, far;

	rep(i, 1, sz(blocks)) {
		auto [len, active] = blocks[i];
		int step = (active ? x : len);
		for (int j = 0; j*step < len; j++) {
			while (near.begin < i && near.mx+j > 1) pop(near);
			while (far.begin < i && far.mx+j > m) pop(far);
			int cnt = min(step, len-j*step);
			ans[x] += (far.get(j) - near.get(j)) * cnt;
			ans[x] += eval(base, m+1) * cnt * far.absPos;
			if (j >= 2) {
				int lim = min(j, m+1);
				ll e = len - j*step, b = e - cnt + 1;
				ans[x] += eval(base, lim) * (e*(e+1) - b*(b-1)) / 2;
			}
		}
		push(near, i);
		push(far, i);
	}
}

void solve(const vector<bool>& seq, int m) {
	vector<pair<int, bool>> blocks = {{0, 0}};
	int n = sz(seq);

	for (int i = 0; i < sz(seq);) {
		int j = i+1;
		while (j < sz(seq) && seq[i] == seq[j]) j++;
		blocks.pb({j-i, 1});
		i = j;
	}

	rep(x, 1, n+1) {
		int j = 1;
		rep(i, 1, sz(blocks)) {
			if (blocks[i].x <= x) blocks[i].y = 0;
			if (j >= 2 && !blocks[i].y && !blocks[j-1].y) {
				blocks[j-1].x += blocks[i].x;
			} else {
				blocks[j++] = blocks[i];
			}
		}
		blocks.resize(j);
		solve(blocks, m, x);
	}
}

void solve(int n, int m) {
	vector<bool> seq;
	int last; cin >> last;
	rep(i, 1, n) {
		int cur; cin >> cur;
		seq.pb(last > cur);
		last = cur;
	}
	solve(seq, m);
}

void run() {
	int n, m;
	cin >> n >> m;

	ans.resize(n+m+1);
	solve(n, m);
	solve(m, n);
	ans[0] = Zp(n) * (n+1) * m * (m+1) / 4;

	cout << "0 ";
	rep(i, 1, n+m) cout << (ans[i-1] - ans[i]).x << ' ';
	cout << '\n';
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(10);
	run();
	cout << flush; _Exit(0);
}