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


using namespace std;


const int MOD = 1'000'000'007;


vector <vector<int>> computeMaxRun(int n, const vector <int> &v) {
    vector <vector<int>> maxRun(2, vector <int> (n + 1));
    for (int incr = 0; incr <= 1; incr++) for (int i = n; i >= 0; i--) {
        if (i == n || incr != (v[i] < v[i + 1])) {
            maxRun[incr][i] = min(i + 1, n);
        } else {
            maxRun[incr][i] = maxRun[incr][i + 1];
        }
    }

    return maxRun;
}

vector <int> solve(int n, int m, vector <int> &a, vector <int> &b) {
    vector <int> ans(n + m, 0);

    auto aRun = computeMaxRun(n, a);
    auto bRun = computeMaxRun(m, b);

    vector <vector<vector<vector<bool>>>> dp(
        n + 1, vector <vector<vector<bool>>> (m + 1, vector <vector<bool>> (2, vector <bool> (2, false)))
    );

    for (int la = 0; la < n; la++) for (int lb = 0; lb < m; lb++) {
        for (int ra = la; ra <= n; ra++) for (int rb = lb; rb <= m; rb++) {
            for (int incr = 0; incr <= 1; incr++) for (int aLast = 0; aLast <= 1; aLast++) {
                dp[ra][rb][incr][aLast] = false;
            }
        }

        for (int incr = 0; incr <= 1; incr++) {
            dp[la][lb][incr][0] = true;
        }

        auto isAny = [&](int x, int y) {
            for (int incr2 = 0; incr2 <= 1; incr2++) for (int aLast2 = 0; aLast2 <= 1; aLast2++) {
                if (dp[x][y][incr2][aLast2]) return true;
            }
            return false;
        };

        int enda = n, endb = m;
        for (int maxStab = 2; maxStab <= n + m; maxStab++) {
            int pairs = 0;

            while (enda >= la && isAny(enda, m)) enda--;
            while (endb >= lb && isAny(n, endb)) endb--;

            for (int ra = la; ra <= n; ra++) {
                int bMax;
                if (ra > enda) {
                    bMax = endb;
                } else {
                    bMax = m;
                }

                for (int rb = lb; rb <= bMax; rb++) {
                    for (int incr = 0; incr <= 1; incr++) for (int aLast = 0; aLast <= 1; aLast++) {
                        if (dp[ra][rb][incr][aLast]) {
                            int last;
                            if (ra == la && rb == lb) {
                                last = -1;
                            } else if (aLast) {
                                last = a[ra - 1];
                            } else {
                                last = b[rb - 1];
                            }

                            if (last == -1 || incr != (last < a[ra])) {
                                for (int ra2 = ra + 1; ra2 <= min(ra + maxStab - 1, aRun[1 - incr][ra]); ra2++) {
                                    dp[ra2][rb][1 - incr][1] = true;
                                }
                            }

                            if (last == -1 || incr != (last < b[rb])) {
                                for (int rb2 = rb + 1; rb2 <= min(rb + maxStab - 1, bRun[1 - incr][rb]); rb2++) {
                                    dp[ra][rb2][1 - incr][0] = true;
                                }
                            }

                            if (last == -1 || incr != (last < (incr ? max(a[ra], b[rb]) : min(a[ra], b[rb])))) {
                                int dMin = max(1, maxStab - 1 - (bRun[1 - incr][rb] - rb));
                                int dMax = min(maxStab - 2, aRun[1 - incr][ra] - ra);

                                for (int d = dMin; d <= dMax; d++) {
                                    int ra2 = ra + d, rb2 = rb + (maxStab - 1 - d);
                                    int aLast2 = (a[ra2 - 1] < b[rb2 - 1]) ^ (1 - incr);

                                    dp[ra2][rb2][1 - incr][aLast2] = true;
                                }
                            }
                        }
                    }
                }
            }

            bool breakHere = enda == la - 1 && endb == lb - 1;
            for (int ra = la + 1; ra <= n; ra++) {
                for (int rb = lb + 1; rb <= m; rb++) {
                    if (isAny(ra, rb)) {
                        int index = maxStab - 1;

                        ans[index]++;
                        if (ans[index] >= MOD) {
                            ans[index] -= MOD;
                        }

                        if (!breakHere && index < n + m - 1) {
                            ans[index + 1]--;
                            if (ans[index + 1] < 0) {
                                ans[index + 1] += MOD;
                            }
                        }
                    }
                }
            }

            if (breakHere) {
                break;
            }
        }
    }

    return ans;
}

int main() {
    ios_base::sync_with_stdio(false);

    int n, m;
    cin >> n >> m;

    vector <int> a(n), b(m);

    for (int &ai : a) {
        cin >> ai; ai--;
    }

    for (int &bi : b) {
        cin >> bi; bi--;
    }

    auto ans = solve(n, m, a, b);
    for (int x : ans) {
        cout << x << ' ';
    }

    return 0;
}