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
#include <cstdio>
#include <cassert>
#include <algorithm>

#define REP(a, n) for (int a = 0; a < (n); ++a)
#define FOR(a, b, c) for (int a = (b); a <= (c); ++a)

typedef long long LL;

using namespace std;

//////////////////////

#define MOD 1'000'000'007

LL L1, L2, H;

int ciag[300005];
LL ile[300005];

void debug_wynd() {
    REP(a, L2+1) ile[a] = 0;
    REP(pocz, L1) {
        int kier = 0, len = 0;
        int c = 0;
        ile[c]++;
        FOR(k, pocz+1, L1-1) {
            int kier2 = ciag[k]>ciag[k-1] ? 1 : -1;
            if (kier2!=kier) { kier = kier2; len = 1;}
            ++len;
            if (len==H) { c++; len = 2; }
            if (c>L2) break;
            ile[c]++;
        }
    }
    FOR(a, 1, L2) ile[a] += ile[a-1];
}

LL res[600010];

void solve() {
    REP(a, L1)
        scanf("%d", ciag+a);
    LL allA = L1*(L1+1)/2%MOD;
    for (H = 3; ; ++H) {
//        fprintf(stderr, "H=%lld\n", H);
        debug_wynd();
        LL rr = 0;
        FOR(c, 1, L2) {
            LL cc = L2+1-c;
            rr = (rr+cc*(allA+MOD-ile[c]%MOD))%MOD;
//            fprintf(stderr, "%d -> %lld\n", c, ile[c]);
        }
        if (!rr) break;
        res[H] = (res[H]+rr)%MOD;
    }
//    fprintf(stderr, "---\n");
}

int main() {
    scanf("%Ld%Ld", &L1, &L2);
    res[1] = res[2] = ((L1*(L1+1)/2)%MOD) * ((L2*(L2+1)/2)%MOD) % MOD;
    REP(a, 2) {
        solve();
        swap(L1, L2);
    }
    FOR(a, 1, L1+L2)
        printf("%lld%c", (res[a]+MOD-res[a+1])%MOD, a==L1+L2 ? '\n' : ' ');
//    printf("\n");
}