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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <bits/stdc++.h>

#define ll long long
#define fors(u, n, s) for(ll u = (s); u < (n); u++)
#define foru(u, n) fors(u, n, 0)
#define vec vector
#define pb push_back
#define f first
#define s second
#define ir(a, b, x) (((a) <= (x)) && ((x) <= (b)))
#define pint pair<ll, ll>

using namespace std;

const int N = 1000;
const int M = 1e9+7;
int a;
int b;

template <int MOD=M>
struct Modular {
  int value;
  static const int MOD_value = MOD;

  Modular(long long v = 0) { value = v % MOD; if (value < 0) value += MOD;}
  Modular(long long a, long long b) : value(0){ *this += a; *this /= b;}

  Modular& operator+=(Modular const& b) {value += b.value; if (value >= MOD) value -= MOD; return *this;}
  Modular& operator-=(Modular const& b) {value -= b.value; if (value < 0) value += MOD;return *this;}
  Modular& operator*=(Modular const& b) {value = (long long)value * b.value % MOD;return *this;}

  Modular& operator/=(Modular const& b) { return *this *= inverse(b); }
  friend Modular operator+(Modular a, Modular const b) { return a += b; }
  friend Modular operator-(Modular a, Modular const b) { return a -= b; }
  friend Modular operator-(Modular const a) { return 0 - a; }
  friend Modular operator*(Modular a, Modular const b) { return a *= b; }
  friend Modular operator/(Modular a, Modular const b) { return a /= b; }
  friend std::ostream& operator<<(std::ostream& os, Modular const& a) {return os << a.value;}
  friend bool operator==(Modular const& a, Modular const& b) {return a.value == b.value;}
  friend bool operator!=(Modular const& a, Modular const& b) {return a.value != b.value;}
};

  Modular<> mexp(Modular<> a, long long e) {
    Modular<> res = 1; while (e) { if (e&1) res *= a; a *= a; e >>= 1; }
    return res;
  }
  Modular<> inverse(Modular<> a) { return mexp(a, M - 2); }

int arr_a[N];
int arr_b[N];

bool infl_a[N];
bool infl_b[N];

int infl_pre_a[N];
int infl_pre_b[N];

vec<int> mem[N][N];
bool calc[N][N];

void solve_segment(int s, int e) {
	if(calc[s][e]) return;
	calc[s][e]=true;
	mem[s][e].clear();
	foru(i, a+b+1) mem[s][e].pb(0);

	if(infl_pre_a[e-1]-infl_pre_a[s] == 0){ // stable case
		fors(i, a+b+1, 1) {
			mem[s][e][i] = max(0LL, (e-s)/i - 1 + ((e-s)%i != 0));
			mem[s][e][i] = min(mem[s][e][i], b+1);
		}
		return;
	}

	int s_ = s+1;
	int e_ = e;
	while(e_ != s_ + 1){
		int m = (e_+s_)/2;
		if(infl_pre_a[m-1] - infl_pre_a[s_-1] != 0) e_ = m;
		else s_ = m;
	}
	int infl = s_;
	if(!infl_a[infl]) cout << "WTH" << endl;

	solve_segment(s, infl);
	solve_segment(infl, e);
	foru(i, a+b+1) {
		mem[s][e][i] = mem[s][infl][i] + mem[infl][e][i];
		mem[s][e][i] = min(mem[s][e][i], b+1);
	}

	//cout << s << " " << e << " debug "; for(auto i : mem[s][e]) cout << i << " "; cout << endl;
}

int stability[N][N];

int get_stability(int s, int e){ // shifted so that its easier to think
	if(stability[s][e] != -1) return stability[s][e];

	if(infl_pre_a[e-1]-infl_pre_a[s] == 0){ // stable case
		stability[s][e] = e-s;
		return stability[s][e];
	}

	int s_ = s+1;
	int e_ = e;
	while(e_ != s_ + 1){
		int m = (e_+s_)/2;
		if(infl_pre_a[m-1] - infl_pre_a[s_-1] != 0) e_ = m;
		else s_ = m;
	}
	int infl = s_;
	
	if(!infl_a[infl]) cout << "WTH" << endl;
	
	stability[s][e] = max(get_stability(s, infl), get_stability(infl, e));
	return stability[s][e];
}

Modular<> out[2*N];

int inv(int a) {
  return a <= 1 ? a : M - (long long)(M/a) * inv(M % a) % M;
}


int main() {

	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> a >> b;

	foru(i, 2*N) out[i]=0;

	foru(i, a) foru(j, a) stability[i][j]=-1;
	foru(i, b) foru(j, b) stability[i][j]=-1;
	
	foru(i, a) cin >> arr_a[i];
	foru(i, b) cin >> arr_b[i];

	fors(i, a-1, 1) if((arr_a[i-1]-arr_a[i])*(arr_a[i]-arr_a[i+1]) < 0) infl_a[i]=true;
	fors(i, b-1, 1) if((arr_b[i-1]-arr_b[i])*(arr_b[i]-arr_b[i+1]) < 0) infl_b[i]=true;

	fors(i, a, 1) infl_pre_a[i] = infl_pre_a[i-1] + infl_a[i];
	fors(i, b, 1) infl_pre_b[i] = infl_pre_b[i-1] + infl_b[i];

	vec<Modular<>> helper_out;
	foru(i, a+b) helper_out.pb(0);

	foru(i, a) fors(j, a, i+1) {
		solve_segment(i, j);
		
		mem[i][j][0]=min(j-i+1, b+1LL);
		fors(k, a+b, 0){
			helper_out[k] -= mem[i][j][k]*(2*b+3);



			helper_out[k] += mexp(mem[i][j][k], 2);

		}

		out[get_stability(i, j)] -= 2*(b+1);
	}

	foru(i, a) foru(j, a) calc[i][j]=false;
	foru(i, a) foru(j, a) stability[i][j]=-1;

	swap(a, b);
	swap(arr_a, arr_b);
	swap(infl_a, infl_b);
	swap(infl_pre_a, infl_pre_b);

	foru(i, a) fors(j, a, i+1) {
		solve_segment(i, j);
		
		mem[i][j][0]=min(j-i+1, b+1LL);
		fors(k, a+b, 0){
			helper_out[k] -= mem[i][j][k]*(2*b+3);

			helper_out[k] += mem[i][j][k]*mem[i][j][k];

		}

		out[get_stability(i, j)] -= 2*(b+1);

	}

	fors(i, a+b, 1) out[i] += helper_out[i] - helper_out[i-1];

	foru(i, a+b) out[i] *= inverse(2);

	fors(i, min(a, b)+1, 1) out[1] += (a-i+1)*(b-i+1);

	foru(i, a+b) cout << out[i] << " "; 
	cout << "\n";

}