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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
/*
for A with some monotonic sequences.
A, B look at bigger of the two?
Can always split to the optimum plus 1.
So best is <= (N/M)+1
Assume N>=M
small edge cases are trashy
2 is doable usually?
1 | 2 | 3
?
1 3 2 4
If string lengths are roughly equal?
a a a b a b
(if you know max s is something?)
You can keep it the same.
if becomes too much:

a a a bad?
a<a<b bad?
a<b>a>a bad?
a<b>a>b bad?
So the best you can do
a a a a a a a a a > b < a?
currently have:
binary search on answer s.
then do dp[i][j][flag] = min current length of monotonic in a direction.

Ok, observation, given lengths N>=M
can add some < or > sign inside N for 1 cost, max cost<=M
What is minimum number of adjacent <<<< signs or >>>> signs?
For a given string: can binary search on s-1 = number of signs, and check it by greedily adding the signs?
If <<<<<< (too many signs), can add > at regular intervals and not at the ends, you need ceil(B/s) signs.
So total number of cost needed is sum of ceil((BLOCK_i)/s)<=M.
So for some s to be optimal you have:  sum ceil((BLOCK_i)/s)<=M < sum ceil((BLOCK_i)/(s-1)).
For some s to be >= optimum? 
You need: sum ceil((BLOCK_i)/s)<=M

And you need: sum from (m-M+1) to N
This is easy to work with?

If you do divide and conquer:
substrings that go over the splitting line: 
s < sqrt(n) ? Can loop over them
s > sqrt(n) Only O(sqrt(n)) segments are important.
I would just like to now all floor((Block_i-1)/s) as a frequency array.
only have O(n/s) important segments, res of the segments contribute 0!.
rest of the segments just contribute something flat.
Want to know contributions of weight1* p_j - p_i*weight2, but only when p_j>p_i
Can do it with a single FFT!
ok, so have important and stupid segments.
stupid segments: can group them together.
can do some FFT?
want to know frequencies of those
Afterwards:
For given s:
now the amount of those?
Can just calc that easily, and subtract with <=(s+1)
inside block need to be careful.
need to subtract those again...
in total <= 3e5*3e5, so can easily fit with regular FFT.
*/
const long long MD = 1e9+7;
template<long long MOD=MD> struct mdint {
    int d;
    mdint () {d=0;}
    mdint (long long _d) : d(_d%MOD){
        if(d<0) d+=MOD;
    };
    friend mdint& operator+=(mdint& a, const mdint& o) {
        a.d+=o.d; if(a.d>=MOD) a.d-=MOD;
        return a;
    }
    friend mdint& operator-=(mdint& a, const mdint& o) {
        a.d-=o.d; if(a.d<0) a.d+=MOD;
        return a;
    }
    friend mdint& operator*=(mdint& a, const mdint& o) {
        return a = mdint((ll)a.d*o.d);
    }
    mdint operator*(const mdint& o) const {
        mdint res = *this;
        res*=o;
        return res;
    }
    mdint operator+(const mdint& o) const {
        mdint res = *this;
        res+=o;
        return res;
    }
    mdint operator-(const mdint& o) const {
        mdint res = *this;
        res-=o;
        return res;
    }
    mdint operator^(long long b) const {
        mdint tmp = 1;
        mdint power = *this;
        while(b) {
            if(b&1) {
                tmp = tmp*power;
            }
            power = power*power;
            b/=2;
        }
        return tmp;
    }
    friend mdint operator/=(mdint& a, const mdint& o) {
        a *= (o^(MOD-2));
        return a;
    }
    mdint operator/(const mdint& o) {
        mdint res = *this;
        res/=o;
        return res;
    }
    bool operator==(const mdint& o) { return d==o.d;}
    bool operator!=(const mdint& o) { return d!=o.d;}
    friend istream& operator>>(istream& c, mdint& a) {return c >> a.d;}
    friend ostream& operator<<(ostream& c, const mdint& a) {return c << a.d;}
};
using  mint = mdint<MD>;
vector<bool> read(int n) {
    vi a(n);
    for(auto& i : a) cin >> i;
    vector<bool> b(n-1);
    for(int i=0;i<n-1;++i) b[i] = a[i]<a[i+1];
    return b;
}
vector<mint> solve(vector<bool> a, int m) {
    int n = a.size();
    // first write n^4!
    vector<mint> ans(n+1); // s<=k, for all k
    for(int len=2;len<=min(n+1,m);++len) {
        for(int olen=len;olen<=m;++olen) {
            ans[0]+=(n+1-len+1)*ll(m-olen+1);
        }
    }
    for(int s=1;s<=n;++s) { // answer is <=s
        mint res=0;
        for(int i=0;i<n;++i) {
            int need=0;
            int cur=1;
            for(int j=i;j<n;++j) {
                if(j>i and a[j]==a[j-1]) cur++;
                else {
                    need+=(cur-1)/s;
                    cur=1;
                }
                int need2 = need + (cur-1)/s;
                // assume substring ends here!
                for(int len=max(1,need2);len<=m;++len) {
                    res+=(m-len+1);
                }
            }

        }
        ans[s]=res;

    }
    // s=k --> s<=k and s>k-1
    for(int i=ans.size()-1;i>0;--i) ans[i]-=ans[i-1];
    ans.erase(ans.begin());
    return ans;
    


}
mint arith(ll a, ll b) {
    if(a>b) return 0;
    return (a+b)*(b-a+1)/2;
}
const mint inv2 = mint(1)/2;
vector<mint> solve2(vector<bool> a, int m) {
    int n = a.size();
    // first write n^4!
    vector<mint> ans(n+1); // s<=k, for all k
    for(int len=2;len<=min(n+1,m);++len) {
        ans[0]+=arith(1,m-len+1)*(n+1-len+1);
    }
    struct B {
        int l;
        int mid;
        int r;
    };
    vector<B> blocks;
    for(int i=0;i<n;) {
        int j = i;
        while(j<n and a[i]==a[j]) ++j;
        blocks.push_back({0,j-i,0});
        i=j;
    }
    for(int s=1;s<=n;++s) { // answer is <=s
        if(!blocks.empty()) {
            vector<B> nb; nb.reserve(blocks.size());
            int freebefore = blocks[0].l;
            for(auto b : blocks) {
                if(b.mid<=s) {
                    freebefore+=b.mid;
                    freebefore+=b.r;
                } else {
                    b.l = freebefore;
                    nb.push_back(b);
                    freebefore = b.r;
                }
            }
            
            if(!nb.empty()) nb.back().r=freebefore;
            for(int i=1;i<nb.size();++i) {
                nb[i-1].r=nb[i].l;
            }
            swap(nb,blocks);
        }
        mint res=0;
        if(blocks.empty()) {
            // all subarrays are good?
            res = arith(1,n)*arith(1,m);
        } else {
            // do it faster!
            mint zeros=0;
            typedef array<mint,3> W;
            vector<W> prefsums = {{}};
            int lastpref=0;
            auto addL = [&](mint num, int mypref) {
                assert(mypref>=lastpref);
                while(lastpref<mypref) {
                    prefsums.push_back(prefsums.back());
                    ++lastpref;
                }
                W nls = {num,num*mypref,num*mypref*mypref};
                auto& ls = prefsums.back();
                for(int i=0;i<3;++i) ls[i]+=nls[i];

            };
            auto addC = [&](mint num, int sum) {
                if(sum==0) zeros+=num;
                res+=num*arith(1,m+1-sum);
            };
            auto addR = [&](mint num, int mypref) {
                W rs = {num,num*mypref,num*mypref*mypref};
                assert(mypref>=lastpref);
                if(mypref==lastpref) {
                    mint cur = prefsums[mypref][0];
                    if(mypref) cur-=prefsums[mypref-1][0];
                    zeros+=cur*num;
                }
                auto ls = prefsums.back();
                int bef = mypref - m-1;
                if(bef>lastpref) return;
                if(bef>=0) {
                    for(int j=0;j<3;++j) ls[j]-=prefsums[bef][j];
                }
                auto contribution = ls[2]*rs[0]*inv2 - ls[1]*rs[1] + ls[1]*rs[0]*(inv2*3+m);
                contribution+=ls[0]*rs[2]*inv2 - rs[1]*ls[0]*(inv2*3+m) + ls[0]*rs[0]*(mint(m)*(m+3)*inv2 + 1);
                res+=contribution; 
            };

            /*
            do just arith(1,m+1-(pb-pa)) =
            Problem when pb-pa>
            a (a/2 - b + m + 3/2) + b (b/2 - m - 3/2) + (m/2 + 3/2) m + 1
            
            and for all 0's do something special?
            */
            int pref=0;
            // in all blocks, inside: 
            // loop over the sum
            // for this sum do the addition manually
            for(auto b : blocks) {
                

                // only strictly inside
                // add the ls.
                addC(arith(1,b.l),0);
                addL(b.l, pref);
                for(int sum=0;sum<=(b.mid-1)/s;++sum) {
                    // strictly inside:
                    int lo = max(1,(sum*s+1)), hi = min(b.mid,(sum+1)*s);
                    mint num = hi-lo+1;
                    if(lo<=hi)
                    addR(num,pref+sum);
                }
                
                for(int sum=0;sum<=(b.mid-1)/s;++sum) {
                    int lo = max(1,(sum*s+1)), hi = min(b.mid,(sum+1)*s);
                    if(lo<=hi) {
                        addC(arith(b.mid+1-hi,b.mid+1-lo),sum);
                    }
                }
                pref+=(b.mid-1)/s;
                for(int sum=(b.mid-1)/s;sum>=0;--sum) {
                    int lo = max(1,sum*s+1), hi = min(b.mid,(sum+1)*s);
                    mint num = hi-lo+1;
                    if(lo<=hi)
                    addL(num,pref-sum);
                }
                addR(b.r,pref);
            }
            addC(arith(1,blocks.back().r), 0);
            res+=zeros*(arith(1,m)-arith(1,m+1));
        //     mint brute=0;
        //     for(int i=0;i<n;++i) {
        //         int need=0;
        //         int cur=1;
        //         for(int j=i;j<n;++j) {
        //             if(j>i and a[j]==a[j-1]) cur++;
        //             else {
        //                 need+=(cur-1)/s;
        //                 cur=1;
        //             }
        //             int need2 = need + (cur-1)/s;
        //             // assume substring ends here!
        //             // need2,1 ? 
        //             brute+=arith(1, m-max(need2,1)+1);
        //         }

        //     }
        //     debug(brute,res);
        //     if(brute!=res) exit(0);
            
        }
        
        ans[s]=res;

    }
    // s=k --> s<=k and s>k-1
    for(int i=ans.size()-1;i>0;--i) ans[i]-=ans[i-1];
    ans.erase(ans.begin());
    return ans;
    


}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m; cin >> n >> m;
    auto a = read(n), b = read(m);
    vector<mint> ans(n+m);
    auto res = solve2(a,m);
    for(int i=0;i<res.size();++i) {
        ans[i]+=res[i];
    }
    res = solve2(b,n);
    for(int i=0;i<res.size();++i) {
        ans[i]+=res[i];
    }
    for(int len=1;len<=min(n,m);++len) {
        // for all with this length, answer is 2:
        ans[0]+=mint(n-len+1)*(m-len+1);
    }
    ans.insert(ans.begin(),0);
    // ans.push_back(0);
    ans.pop_back();
    // assert(ans.size()==n+m);
    cout << ans << ' ' << '\n';

}