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

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using vii = vector<pii>;

const int mod = 1e9 + 7;

inline int sufit(const int& a, const int& b) {
  return (a + b - 1) / b;
}

struct Info {
  int min_len, time;

  Info() {}

  Info(int min_len_, int time_) : min_len(min_len_), time(time_) {}
};

const int N = 3e5 + 10;
ll ans[N + N];
ll weird_sum[N];
vi divs[N];

ll interval_count(ll min_len, ll max_len, ll m) {
  if (max_len < min_len) {
    return 0;
  }
  return ((m - min_len + 1LL) * (m - min_len + 2LL) -
          (m - max_len) * (m - max_len + 1LL)) /
         2LL;
}

ll get_to_update(int beg, int end, int min_len, int b_len, int m) {
  ll res = (ll)(end - beg) * (interval_count(max(1, min_len), b_len, m));
  res %= mod;

  return res;
}

struct Monotonic {
  map<int, Info> m;

  Monotonic(int time) { m[2] = {0, time}; }

  void update(int stab, Info info, int time, int b_len, int n) {
    if (stab == 2) {
      return;
    }
    // if (info.min_len == 0 && b_len == 1) {
    //   return;
    // }
    b_len = min(b_len, n);
    ll to_update = get_to_update(info.time, time, info.min_len, b_len, n);
    // cout << "Update time!! " << stab << "\nlast update: " << info.time
    //      << "\ncurrent time: " << time << "\nfrom " << max(1, info.min_len)
    //      << " to " << b_len << " and m=" << n << " -> " << to_update << "\n";
    // cout << "\n";

    ans[stab] += to_update;
    ans[stab] %= mod;
  }

  void maybe_add(int stab, int min_len, int time, int b_len, int n) {
    /* We need to split the next stability into a new segment. */
    if (!m.count(stab + 1)) {
      m[stab + 1] = {m[stab].min_len, time - 1};
    } else if (m[stab + 1].time != time - 1) {
      // cout << "kazano mi\n";
      update(stab + 1, m[stab + 1], time - 1, min_len - 2, n);
      m[stab + 1].time = time - 1;
    }

    Info info = m[stab];
    m[stab].min_len++;
    m[stab].time = time - 1;

    auto it = m.find(stab);
    if (it != m.begin()) {
      it--;

      if (it->second.min_len == min_len) {
        m.erase(stab);
      }

      b_len = it->second.min_len - 1;
    }
    update(stab, info, time - 1, b_len, n);
  }
};

/* k is the desired stability, returns the minimal length
   of the other array to get this stability. */
int minimal_count(int k, const vi& a, int from, int to) {
  int cnt = 0;
  for (int i = from; i < to - 1;) {
    // int len = 0;
    int idx = i + 1;
    bool yes = false;
    while (idx < to && a[idx] > a[idx - 1]) {
      yes = true;
      idx++;
    }
    while (!yes && idx < to && a[idx] < a[idx - 1]) {
      idx++;
    }
    cnt += sufit(max(0, idx - i - k), k - 1);
    i = idx - 1;
  }

  return cnt;
}

int min_cnt_for_mon(int k, int n) {
  return sufit(max(0, n - k), k - 1);
}

void count_with_shorter(int n, int m, const vi& a) {
  for (int i = 0; i < n - 1; i++) {
    vi min_lens_for_stability(n + 1);
    /* The case of 2-element prefix can be just solved like that, all 1-element
       subarrays of b are good. */
    ans[2] += m;
    ans[2] %= mod;
    bool incr = a[i + 1] > a[i];
    int cur_mon_suffix_len = 2;
    Monotonic monotonic(i);

    /* Intervals of length 1 and 2 are considered separately */
    for (int j = i + 2; j < n; j++) {
      // cout << ">>> " << i << " " << j << "\n";
      int len = j - i + 1;
      vii min_lens;
      // int cnt_changed = 0;

      if ((incr && a[j] > a[j - 1]) || (!incr && a[j] < a[j - 1])) {
        cur_mon_suffix_len++;
        for (auto d : divs[cur_mon_suffix_len - 2]) {
          min_lens_for_stability[d + 1]++;
          monotonic.maybe_add(d + 1, min_lens_for_stability[d + 1], j,
                              min(m, len - 1), m);
        }
      } else {
        incr = !incr;
        cur_mon_suffix_len = 2;
      }

      int b_len = min(m, len - 1);
      // cout << "Dumping monotonic\n";
      for (auto p : monotonic.m) {
        int stab = p.first;
        int min_len = p.second.min_len;
        // cout << stab << " " << min_len << "\n";
        if (p.first == 2) {
          ans[stab] += interval_count(max(1, min_len), b_len, m);
          ans[stab] %= mod;

          /* UNCOMMENT THIS LATER!!!!!! */
          break;
        }
      }
    }

    // cout << "Last try:\n";
    int b_len = min(m, n - i);
    for (auto p : monotonic.m) {
      if (p.first != 2) {
        monotonic.update(p.first, p.second, n - 1, b_len, m);
      }
      b_len = min(m, p.second.min_len - 1);
    }

    // cout << i << " ans: "
    //      << "\n";
    // for (int j = 1; j <= n; j++) {
    //   cout << ans[j] << " ";
    // }
    // cout << "\n";
  }
}

vi a, b;
int n, m;

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

  cin >> n >> m;
  a.resize(n);
  b.resize(m);

  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> b[i];
  }

  int mx = max(m, n);
  for (int i = 1; i <= mx; i++) {
    weird_sum[i] = weird_sum[i - 1] + interval_count(mx - i + 1, mx, mx);
    weird_sum[i] %= mod;
  }

  for (int i = 1; i <= max(m, n); i++) {
    for (int j = i; j <= max(m, n); j += i) {
      divs[j].push_back(i);
    }
  }

  count_with_shorter(n, m, a);
  count_with_shorter(m, n, b);

  for (int i = 1; i <= min(n, m); i++) {
    ans[2] += (ll)(n - i + 1) * (m - i + 1);
    ans[2] %= mod;
  }

  for (int i = 1; i <= n + m; i++) {
    cout << ans[i] << " ";
  }
  cout << "\n";
}