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
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;

// N^2 Log
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define losuj(a, b) uniform_int_distribution<long long>(a, b)(rng)

#define FOR(i, n) for (int i = 0; i < n; i++)
#define f first
#define s second
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()

using vi = vector<int>;
using ll = long long;
using pll = pair<ll, ll>;
using vll = vector<ll>;
using ii = pair<int, int>;

const ll BIG_INF = 1e18;
const int INF = 1e9 + 7;

int ile_potrz(int n, int len)
{
    int q = n / (len - 1);
    int mod = n - q * (len - 1);

    if (mod <= 1)
    {
        return q - 1;
    }
    return q;
}

vector<vector<int>> zmiany;

void prep(int len)
{
    // 0-2 demandy to wszędzie 0
    zmiany.resize(len + 1);

    for (int i = 3; i <= len; i++)
    {
        for (int j = 2; j <= len; j++)
        {
            int poprz = ile_potrz(i - 1, j);
            int teraz = ile_potrz(i, j);
            if (poprz != teraz)
            {
                zmiany[i].pb(j);
            }
        }
    }
}

void solve()
{
    int n, m;
    cin >> n >> m;
    prep(max(n, m) + 3);

    vi a(n), b(m);
    for (auto &u : a)
    {
        cin >> u;
    }
    for (auto &u : b)
    {
        cin >> u;
    }

    vll ans(n + m + 1, 0);

    FOR (phase, 2)
    {   
        n = sz(a);
        m = sz(b);

        for (int pocz = 0; pocz < n; ++pocz)
        {
            vector<int> sigma_demand(n - pocz + 2, 0);
            vector<vector<ii>> pointers(n - pocz + 2);
            vector<int> pointers_start(n - pocz + 2);

            int last_range_len = 2;

            for (int kon = pocz; kon < n; ++kon)
            {
                if (kon - pocz + 1 <= m)
                {
                    pointers[2].pb({kon - pocz + 1, kon});
                }
                if (pocz + 1 >= kon)
                {
                    continue;
                }

                if ((a[kon] > a[kon - 1]) != (a[kon - 1] > a[kon - 2]))
                {
                    last_range_len = 2;
                }
                else
                {
                    last_range_len++;
                    for (auto & change : zmiany[last_range_len])
                    {
                        sigma_demand[change]++;
                        if (pointers_start[change] != sz(pointers[change]) and pointers[change][pointers_start[change]].first < sigma_demand[change])
                        {
                            auto [len, when] = pointers[change][pointers_start[change]];
                            ++pointers_start[change];
                            ans[change] += (kon - when) * (m - len + 1);
                            pointers[change + 1].pb({len, kon});
                        }
                    }
                }
            }
            for (int ans_len = 2; ans_len < sz(pointers); ++ans_len)
            {
                for (int i = pointers_start[ans_len]; i < sz(pointers[ans_len]); ++i)
                {
                    auto [len, when] = pointers[ans_len][i];
                    ans[ans_len] += (n - when) * (m - len + 1);
                }
            }
        }
        swap(a, b);
    }
    for (int i = 1; i <= min(n, m); i++)
    {
        ans[2] -= (n - i + 1) * (m - i + 1);
    }
    
    for (int i = 1; i <= n + m; i++)
    {
        cout << ans[i] % INF << ' ';
    }
    cout << '\n';
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    int tests = 1;
    // cin >> tests;

    for (int test = 1; test <= tests; test++)
    {
        solve();
    }

    return 0;
}