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
#include <bits/stdc++.h>
using std::cin, std::cout, std::vector;

//===

void init_io() {
  cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
}

template<unsigned MOD>
class Modulo {
public:
  constexpr Modulo(unsigned x=0):v(x) {}
  unsigned get() const { return v; }
  Modulo operator+(Modulo b) const {
    unsigned res = v+b.v;
    if (res >= MOD) res -= MOD;
    return res;
  }
  void operator+=(Modulo b) { *this = *this + b; }
  Modulo operator-(Modulo b) const { return *this + Modulo(MOD-b.v); }
  void operator-=(Modulo b) { *this = *this - b; }
  Modulo operator*(Modulo b) const { return Modulo(std::uint64_t(v) * b.v % MOD); }
  void operator*=(Modulo b) { *this = *this * b; }
private:
  unsigned v;
};

//===

using Mod = Modulo<1'000'000'007>;
constexpr Mod inv2 = Mod(500'000'004);

// Returns a sequence of 0s and 1s
vector<char> read_text(const unsigned n) {
  vector<char> v;
  v.reserve(n);
  unsigned x;
  cin >> x;
  for (unsigned i = 0; i < n; ++i) {
    unsigned y;
    cin >> y;
    v.push_back(y > x);
    x = y;
  }
  return v;
}

Mod sum_below(const unsigned n) {
  return Mod(n) * (Mod(n) - Mod(1)) * inv2;
}

vector<vector<unsigned>> divisors;

void calc_divisors(const unsigned max_n) {
  divisors.resize(max_n+1);

  for (unsigned d=1; d <= max_n; ++d) {
    for (unsigned x=d; x <= max_n; x += d) {
      divisors[x].push_back(d);
    }
  }
}

Mod all_possibilities(const unsigned Alen, const unsigned Blen) {
  // Includes empty sequences of +-.
  return sum_below(Alen+2) * sum_below(Blen+2);
}

// Calculates pairs that are invalid.
class Calculator {
public:
  Calculator(const unsigned Alen1, const unsigned max_points1):
    Alen(Alen1),
    max_points(max_points1)
  {}

  Mod final_count() const {
    return count;
  }

  void add_floating_point(const unsigned j, const unsigned distance) {
    if (floating_points.size() == max_points) return;
    count -= current_count_from(j);

    floating_points.push_back(distance);
    sum_i_floating += Mod(distance) * Mod(floating_points.size()-1);
    sum_floating += Mod(distance);

    if (frozen_points.size() + floating_points.size() - frozen_points_start > max_points) {
      sum_frozen -= Mod(frozen_points[frozen_points_start]);
      sum_i_frozen -= Mod(frozen_points.size() - frozen_points_start - 1) *
                      Mod(frozen_points[frozen_points_start]);
      ++frozen_points_start;
    }

    count += current_count_from(j);
  }

  void freeze_floating_points(const unsigned j) {
    assert(!floating_points.empty());
    const unsigned num_floating = floating_points.size();

    count -= current_count_from(j);

    sum_i_frozen += Mod(num_floating) * sum_frozen;
    sum_i_frozen += Mod(j) * sum_below(num_floating) - sum_i_floating;
    sum_frozen += Mod(j) * Mod(num_floating) - sum_floating;

    for (auto it = floating_points.rbegin(); it != floating_points.rend(); ++it) {
      frozen_points.push_back(j - *it);
    }
    floating_points.clear();
    sum_i_floating = 0;
    sum_floating = 0;

    count += current_count_from(j);
  }

private:
  Mod current_count_from(const unsigned j) const {
    const unsigned remaining = Alen+1-j;

    Mod result = 0;
    const unsigned num_floating = floating_points.size();
    if (num_floating != 0) {
      result +=
        (Mod(num_floating) * Mod(max_points) - sum_below(num_floating)) *
        (Mod(remaining)*Mod(Alen) - sum_below(remaining));
      result += Mod(remaining) * (
          sum_i_floating - Mod(max_points) * sum_floating);
    }

    const unsigned num_frozen = frozen_points.size() - frozen_points_start;
    if (num_frozen != 0) {
      result += Mod(remaining) *
        (Mod(max_points-num_floating) * sum_frozen - sum_i_frozen);
    }

    // Undo the first point.
    if (num_floating != 0) {
      result -= Mod(max_points) *
          (Mod(remaining) * Mod(Alen-floating_points[0]) - sum_below(remaining));
    } else if (num_frozen != 0) {
      result -= Mod(max_points) * Mod(remaining) * Mod(frozen_points.back());
    }

    return result;
  }

  unsigned Alen;
  unsigned max_points;
  Mod count = 0;

  vector<unsigned> frozen_points;
  unsigned frozen_points_start = 0;
  vector<unsigned> floating_points; // distances from j

  Mod sum_frozen = 0; // sum frozen[i]
  Mod sum_i_frozen = 0; // sum i * frozen[i] from the back

  Mod sum_floating = 0; // sum of distances
  Mod sum_i_floating = 0; // sum i * floating[i] from the back
};

void add_invalid_one_side(const vector<char> &A, const unsigned max_points, vector<Mod> &counts) {
  const unsigned Alen = A.size();

  vector<Calculator> calculators;
  calculators.reserve(counts.size());
  for (unsigned k=0; k<counts.size(); ++k) {
    calculators.emplace_back(Alen, max_points);
  }

  char last = '?';
  unsigned run_len = 0;
  for (unsigned j = 0; j < Alen; ++j) {
    if (A[j] != last) {
      for (unsigned k = 1; k < run_len; ++k) {
        calculators[k].freeze_floating_points(j);
      }
      run_len = 0;
      last = A[j];
    }
    ++run_len;

    if (run_len >= 2) {
      for (const unsigned k : divisors[run_len-1]) {
        calculators[k].add_floating_point(j+1, run_len-1);
      }
    }
  }

  for (unsigned k=1; k<counts.size(); ++k) {
    counts[k] += calculators[k].final_count();
  }
}

vector<Mod> solve(const vector<char> &A, const vector<char> &B) {
  const unsigned Alen = A.size();
  const unsigned Blen = B.size();

  // counts[k]: count illegals such that make max run <= k impossible
  vector<Mod> counts(Alen + Blen + 2, Mod(0));

  add_invalid_one_side(A, Blen+2, counts);
  add_invalid_one_side(B, Alen+2, counts);

  // Count valid max run == k.
  for (unsigned i=counts.size()-1u; i>=2u; --i) {
    counts[i] = counts[i-1] - counts[i];
  }
  counts[1] = all_possibilities(Alen, Blen) - counts[1];

  return counts;
}

int main() {
  init_io();

  unsigned Alen, Blen;
  cin >> Alen >> Blen;
  --Alen;
  --Blen;
  const vector<char> A = read_text(Alen);
  const vector<char> B = read_text(Blen);

  calc_divisors(std::max(Alen, Blen));

  const vector<Mod> res = solve(A, B);
  for (const Mod x : res) {
    cout << x.get() << ' ';
  }
  cout << "\n";
}