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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#include <bits/stdc++.h>
#define MP make_pair
#define PB push_back
#define int long long
#define st first
#define nd second
#define rd third
#define FOR(i, a, b) for(int i =(a); i <=(b); ++i)
#define RE(i, n) FOR(i, 1, n)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define REP(i, n) for(int i = 0;i <(n); ++i)
#define VAR(v, i) __typeof(i) v=(i)
#define FORE(i, c) for(VAR(i, (c).begin()); i != (c).end(); ++i)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define __builtin_ctz __builtin_ctzll
#define __builtin_clz __builtin_clzll
#define __builtin_popcount __builtin_popcountll
using namespace std;
template<typename TH> void _dbg(const char* sdbg, TH h) { cerr<<sdbg<<"="<<h<<"\n"; }
template<typename TH, typename... TA> void _dbg(const char* sdbg, TH h, TA... t) {
  while(*sdbg != ',') { cerr<<*sdbg++; } cerr<<"="<<h<<","; _dbg(sdbg+1, t...);
}
#ifdef LOCAL
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#define debugv(x) {{cerr <<#x <<" = "; FORE(itt, (x)) cerr <<*itt <<", "; cerr <<"\n"; }}
#else
#define debug(...) (__VA_ARGS__)
#define debugv(x)
#define cerr if(0)cout
#endif
#define next ____next
#define prev ____prev
#define left ____left
#define hash ____hash
typedef long long ll;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VLL;
typedef vector<pair<int, int> > VPII;
typedef vector<pair<ll, ll> > VPLL;

template<class C> void mini(C&a4, C b4){a4=min(a4, b4); }
template<class C> void maxi(C&a4, C b4){a4=max(a4, b4); }
template<class T1, class T2>
ostream& operator<< (ostream &out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")";}
template<class A, class B, class C> struct Triple { A first; B second; C third;
  bool operator<(const Triple& t) const { if (st != t.st) return st < t.st; if (nd != t.nd) return nd < t.nd; return rd < t.rd; } };
template<class T> void ResizeVec(T&, vector<int>) {}
template<class T> void ResizeVec(vector<T>& vec, vector<int> sz) {
  vec.resize(sz[0]); sz.erase(sz.begin()); if (sz.empty()) { return; }
  for (T& v : vec) { ResizeVec(v, sz); }
}
typedef Triple<int, int, int> TIII;
template<class A, class B, class C>
ostream& operator<< (ostream &out, Triple<A, B, C> t) { return out << "(" << t.st << ", " << t.nd << ", " << t.rd << ")"; }
template<class T> ostream& operator<<(ostream& out, vector<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class T> ostream& operator<<(ostream& out, set<T> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }
template<class L, class R> ostream& operator<<(ostream& out, map<L, R> vec) { out<<"("; for (auto& v: vec) out<<v<<", "; return out<<")"; }


const int P = 1e9 + 7;
const int N = 6e5 + 5;
int t1[N];
int t2[N];

struct S {
  int S0, S1, S2, m;
  int active_zeros; 
  int subtracted;
  VPII que;
  int nxt_in, nxt_out;
  S(int mm) {
    m = mm;
    S0 = S1 = S2 = 0;
    subtracted = 0;
    nxt_in = nxt_out = 0;
    active_zeros = 0;
    //debug("init");
  }
  void CheckFingers() {
    while (nxt_in < SZ(que) && que[nxt_in].st - subtracted <= m) {
      AppendInner(que[nxt_in].st - subtracted, que[nxt_in].nd);
      nxt_in++;
    }
    while (nxt_out < SZ(que) && que[nxt_out].st == subtracted) {
      //debug(nxt_out, que[nxt_out]);
      active_zeros += que[nxt_out].nd;
      RemInner(0, que[nxt_out].nd);
      nxt_out++;
    }
  }
  void AppendOuter(int x, int w) {
    que.PB({x, w});
    CheckFingers();
  }
  void RemOuter(int x, int w) {
    assert(x >= subtracted);
    if (x == subtracted) {
      active_zeros -= w;
      return;
    } else {
      
      PII parka{x, w};
      assert(nxt_out < SZ(que) && que[nxt_out] == parka);
      //debug(x, w, que[nxt_out]);
      if (nxt_out == nxt_in) {
        AppendInner(que[nxt_in].st - subtracted, que[nxt_in].nd);
        nxt_in++;
      }
      RemInner(x - subtracted, w);
      nxt_out++;
    }
  }
  void AppendInner(int x, int w) {
    //assert(x <= m + 1);
    //debug("app", x, w);
    S0 = S0 + w;
    S1 = (S1 + w * x) % P;
    S2 = (S2 + w * x * x) % P;
    //debug(S1, S0);
    if (S1 < 0) { S1 += P; }
    if (S2 < 0) { S2 += P; }
  }
  void RemInner(int x, int w) {
    AppendInner(x, -w);
  }
  void Sub(int x) {
    //debug("shift", x);
    subtracted += x;
    active_zeros = 0;
    S2 = (S2 - 2 * x * S1 + x * x * S0 + 5 * P) % P;
    S1 = (S1 - S0 * x + 5 * P) % P;
    //debug(S1, S0);
    CheckFingers();
  }
  int Eval() {
    debug(S0, S1, S2, active_zeros);
    int heh = S0 * (m + 1) % P * (m + 2) % P + S2 - S1 * (2 * m + 3);
    heh += (m * (m + 1)) % P * active_zeros % P;
    heh %= P;
    if (heh < 0) { heh += P; }
    return heh * ((P + 1) / 2) % P;
  }
};

struct SBrut {
  VPII zbiorek;
  int m;
  int subtracted;
  SBrut(int mm) {
    m = mm;
    subtracted = 0;
  }
  void AppendOuter(int x, int w) {
    zbiorek.PB({x, w});
  }
  void RemOuter(int x, int w) {
    x -= subtracted;
    auto it = find(ALL(zbiorek), PII{x, w});
    //auto it = zbiorek.find({x, w});
    assert(it != zbiorek.end());
    zbiorek.erase(it);
  }
  void Sub(int x) {
    subtracted++;
    for (auto& p : zbiorek) {
      p.st -= x;
    }
  }
  int Eval() {
    int res = 0;
    for (auto p : zbiorek) {
      int gora = min(m, max(m - p.st + 1, 0ll));
      res = (res + gora * (gora + 1) / 2 * p.nd) % P;
    }
    return res;
  }
};

int glob[N];
int n, m;
int Pref(int t) {
  if (t <= 0) { return 0; }
  return t * (t + 1) / 2 % P;
}
void Solve() {
  VI ls;
  int cur_asc = 1, cur_desc = 1;
  FOR (i, 2, n) {
    if (t1[i] > t1[i - 1]) {
      if (cur_desc > 1) {
        ls.PB(cur_desc);
      }
      cur_asc++;
      cur_desc = 1;
    } else {
      if (cur_asc > 1) {
        ls.PB(cur_asc);
      }
      cur_asc = 1;
      cur_desc++;
    }
  }
  VI res(n + m + 1);
  ls.PB(cur_asc + cur_desc - 1);
  VPII bd;
  int st = 1;
  for (auto x : ls) {
    bd.PB({st, st + x - 1});
    st += x - 1;
  }
  assert(st == n);
  VVI inds(n + 2);
  REP (i, SZ(ls)) {
    int x = 2;
    while (ls[i] - 2 >= x - 1) {
      inds[x].PB(i);
      x++;
    }
  }
  debug(ls);
  int start = 0;
  FOR (A, 2, min(n, m)) {
    int ile = n - A + 1;
    int R = A - 1;
    start = (start + (m - R) * (m - R + 1) / 2 % P * ile) % P;
  }
  debug(start);
  FOR (x, 2, n) {
    res[x] = (res[x] - start + P) % P;
    debug(x);
    function<int(int)> Dziura = [&](int l) {
      int ret = l * (l - 1) / 2 % P * Pref(m) % P;
      debug(ret);
      return ret;
    };
    function<int(int)> Linia = [&](int l) {
      int mi = 2;
      int L = 1;
      int here = 0;
      while (mi <= l) {
        int ma = min(mi + x - 2, l);
        int wybA = (Pref(l - mi + 1) - Pref(l - ma) + P) % P;
        here = (here + wybA * Pref(m - L + 1)) % P;
        if (mi != 2) { L++; }
        mi = ma + 1;
      }
      debug(here);
      return here;
    };
        
    if (inds[x].empty()) {
      res[x] = (res[x] + Dziura(n)) % P;
      //assert(0); // FIXME      
    } else {
      res[x] = (res[x] + Dziura(bd[inds[x][0]].st)) % P;
      res[x] = (res[x] + Dziura(n - bd[inds[x].back()].nd + 1)) % P;
      for (int i = 0; i < SZ(inds[x]); i++) {
        PII p = bd[inds[x][i]];
        res[x] = (res[x] + Linia(p.nd - p.st + 1)) % P;
        if (i + 1 < SZ(inds[x])) {
          res[x] = (res[x] + Dziura(bd[inds[x][i + 1]].st - p.nd + 1)) % P;
        }
      }
      if (x == 2) {
        cerr<<"dziury i linie\n";
        debug(res[2]);
      }
      
      S zbiorek_fast(m);
#ifdef LOCAL
      SBrut zbiorek_slow(m);
#endif
      VPII adds;
      int last = bd[inds[x][0]].st;
      int cur = 0;
      debug(cur, last);
      adds.PB({0, last});
      for (auto ind : inds[x]) {
        auto intv = bd[ind];
        //debug(intv);
        if (last != intv.st) {
          debug(cur, intv.st - last);
          adds.PB({cur, intv.st - last});
        }
        last = intv.st;
        while (last + x - 1 <= intv.nd) {
          debug(cur, x - 1);
          adds.PB({cur, x - 1});
          cur++;
          last += x - 1;
        }
        if (last != intv.nd) {
          debug(cur, intv.nd - last);
          adds.PB({cur, intv.nd - last});
          last = intv.nd;
        } else {
          cur--;
        }
      }
      if (last != n) {
        debug(cur, n - last);
        adds.PB({cur, n - last});
      }
      for (auto add : adds) {
        zbiorek_fast.AppendOuter(add.st, add.nd);
#ifdef LOCAL
        zbiorek_slow.AppendOuter(add.st, add.nd);
#endif
      }
      debug("done adding");
      int fir = 1;
      int fin = 0;
      //cur = 0;
      function<void()> Progress = [&]() {
        //adds[fin].st -= cur;
        zbiorek_fast.RemOuter(adds[fin].st, adds[fin].nd);
#ifdef LOCAL
        zbiorek_slow.RemOuter(adds[fin].st, adds[fin].nd);
#endif
        last += adds[fin].nd;
        fin++;
      };
      last = 0;
      for (auto ind : inds[x]) {
        auto intv = bd[ind];
        if (last != intv.st) {
          Progress();
          int len = intv.st - fir;
          int ev = zbiorek_fast.Eval();
          #ifdef LOCAL
          int check = zbiorek_slow.Eval();
          assert(ev == check);
          #endif
          res[x] = (res[x] + ev * len) % P;
          fir = intv.st;
        }
        int gathered = 0;
        while (gathered < intv.nd - intv.st) {
          gathered += adds[fin].nd;
          Progress();
        }
        assert(gathered == intv.nd - intv.st);
        int len = (intv.nd - intv.st) % (x - 1);
        if (len == 0) { len = x - 1; }
        int ev = zbiorek_fast.Eval();
#ifdef LOCAL
        int check = zbiorek_slow.Eval();
        assert(ev == check);
#endif
        res[x] = (res[x] + ev * len) % P;
        fir += len;
        while (fir < intv.nd) {
          zbiorek_fast.Sub(1);
#ifdef LOCAL
          zbiorek_slow.Sub(1);
#endif
          ev = zbiorek_fast.Eval();
          //check = zbiorek_slow.Eval();
          //debug(ev, check);
          //assert(ev == check);
          len = x - 1;
          res[x] = (res[x] + ev * len) % P;
          fir += x - 1;
        }
        assert(fir == intv.nd);
      }
      
    }
  }
  FOR (i, n + 1, n + m) {
    res[i] = res[i - 1];
  }
  debug(res);
  RE (i, n + m) {
    glob[i] = (glob[i] + res[i]) % P;
  }
}

int32_t main() {

  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(10);
  cerr << fixed << setprecision(10);
  cin.tie(0);
  //double beg_clock = 1.0 * clock() / CLOCKS_PER_SEC;
  
  cin>>n>>m;
  RE (i, n) {
    cin>>t1[i];
  }
  RE (i, m) {
    cin>>t2[i];
  }
  Solve();
  RE (i, max(n, m)) {
    swap(t1[i], t2[i]);
  }
  swap(n, m);
  Solve();
  RE (i, n + m) {
    cerr<<glob[i]<<" ";
  }
  cerr<<endl;
  FORD (i, n + m, 1) {
    glob[i] = (glob[i] - glob[i - 1] + P) % P;
  }
  RE (L, min(n, m)) {
    glob[2] = (glob[2] + (n - L + 1) * (m - L + 1)) % P;
  }
  RE (i, n + m) {
#ifdef LOCAL
    assert(glob[i] < P / 2);
#endif
    cout<<glob[i]<<" ";
  }
  cout<<endl;
      
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  return 0;
}