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
#include <bits/stdc++.h>

using namespace std;
using LL = long long;
#define FOR(i, l, r) for(int i = (l); i <= (r); ++i)
#define REP(i, n) FOR(i, 0, (n) - 1)
#define ssize(x) int(x.size())

template<class A, class B> auto &operator<<(ostream &o, pair<A, B> p) {
    return o << '(' << p.first << ", " << p.second << ')';
}

template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) {
    o << '{'; int i = 0; for (auto e : x) o << (", ")+2*!i++ << e;
    return o << '}';
}

#ifdef DEBUG
#define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n';
#else
#define debug(...) {}
#endif


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    constexpr LL mod = (LL) (1e9 + 7);
    int n, m;
    cin >> n >> m;
    vector<int> a(n), b(m);
    REP(i, n)
        cin >> a[i];
    REP(i, m)
        cin >> b[i];

    auto set_mono = [&]() {
        vector<bool> mono(n);
        mono[0] = true;
        if (n == 1)
            return mono;

        bool inc = a[1] > a[0];
        mono[1] = true;
        FOR(i, 2, n - 1) {
            if (inc && a[i] > a[i - 1]) {
                mono[i] = true;
            }
            else if (!inc && a[i] < a[i - 1]) {
                mono[i] = true;
            }
            else {
                mono[i] = false;
                inc = !inc;
            }
        }

        return mono;
    };

    vector<LL> ans(n + m + 1);
    vector<int> ans2(n + m + 1);

    auto solve_for_a = [&]() {
        auto mono = set_mono();
        REP(i, n) {
            vector<int> seq = {2};
            FOR(j, i + 1, n - 1) {
                if (j != i + 1) {
                    if (mono[j])
                        seq.back()++;
                    else
                        seq.emplace_back(2);
                }

                for (auto &it : seq) {
                    FOR(k, 2, it - 1)
                        ans2[k] += (it - 2) / (k - 1);
                }

                int prev_bound = min(m + 1, j - i + 1);
                FOR(k, 2, n + m) {
                    if (prev_bound == 0)
                        break;
                    if (ans2[k] >= prev_bound)
                        continue;
                    FOR(kk, max(ans2[k], 1), prev_bound - 1) {
                        ans[k] += m - kk + 1;
                        if (ans[k] >= mod)
                            ans[k] -= mod;
                    }
                    prev_bound = ans2[k];
                }

                FOR(k, 2, n + m)
                    ans2[k] = 0;
            }
        }
    };

    // Consider equal length segments (to avoid double-counting).
    REP(i, min(n, m)) {
        ans[2] += (LL) (n - i) * (m - i);
        ans[2] %= mod;
    }
    solve_for_a();
    swap(a, b);
    swap(n, m);
    solve_for_a();

    ans[2] += ans[1];
    ans[2] %= mod;
    ans[1] = 0;
    FOR(i, 1, n + m)
        cout << ans[i] << ' ';
    cout << '\n';

    return 0;
}