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
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<(int)b;++i)
#define FORD(i,a,b) for(int i=a;i>=(int)b;--i)
#define PB push_back
#define EB emplace_back
#define FI first
#define SE second
#define umap unordered_map
#define uset unordered_set
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
#define vvll vector<vll>
#define vpii vector<pii>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ALL(X) (X).begin(),(X).end()
#ifndef DEBUG
#define endl (char)10
#endif
using namespace std;
using ll = long long;
using ld = long double;

template <class T>
istream& operator>> (istream& is, vector<T>& vec){
    FOR(i,0,vec.size()) is >> vec[i];
    return is;
}
template <class T>
ostream& operator<< (ostream& os, vector<T>& vec){
    for(auto& t : vec) os << t << " ";
    return os;
}
template<class T, class U>
ostream& operator<< (ostream& os, const pair<T, U>& p){
    os << p.FI << " " << p.SE;
    return os;
}
template<class T, class U>
istream& operator>> (istream& is, pair<T, U>& p){
    is >> p.FI >> p.SE;
    return is;
}

const ll MOD = 1000000007;

void ins(map<int, int>& m, int k, int v){
    if (m.empty()){
        m[k] = v;
        return;
    }
    auto it = m.upper_bound(k);
    if (it != m.begin()){
        it--;
        if (it -> SE <= v) return;
        it++;
    }
    while(it != m.end() && it -> SE >= v) it = m.erase(it);
    m[k] = v;

}

int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int n, m;
    cin >> n >> m;
    vi A(n), B(m);
    cin >> A >> B;
    vll ans(n + m, 0);
    vector<vector<vector<map<int, int>>>> dp(n + 1,
        vector<vector<map<int, int>>>(m + 1,
            vector<map<int, int>>(4
    )));
    //int p = 0, q = 2;
    FOR(p,0,n){
        FOR(q,0,m){
            FOR(i,p,n+1) FOR(j,q,m+1) FOR(k,0,4) dp[i][j][k].clear();

            { // LR
                int d = A[p] < B[q];
                dp[p + 1][q + 1][2 * d + 1][2] = 2;
            }
            { // RL
                int d = B[q] < A[p];
                dp[p + 1][q + 1][2 * d][2] = 2;
            }
            if(p < n - 1) { // LL
                int d = A[p] < A[p + 1];
                dp[p + 2][q][2 * d][2] = 2;
            }
            if(q < m - 1) { // RR
                int d = B[q] < B[q + 1];
                dp[p][q + 2][2 * d + 1][2] = 2;
            }

            int klim = n + m - p - q;
            FOR(k,2,klim) {
                FOR(iq,max(0, p + k - n),k+1){
                    int nq = q + iq;
                    if(nq > m) break;
                    int np = p + k - iq;
                    FOR(sss,0,4){
                        if(dp[np][nq][sss].empty()) continue;
                        int d = sss / 2;
                        int kol = sss % 2;
                        int pv = (kol == 0 ? A[np - 1] : B[nq - 1]);
                        if(np < n){
                            int nd = (pv < A[np]);
                            for(auto& ppp : dp[np][nq][sss]){
                                int cur_val = (d == nd ? ppp.SE + 1 : 2);
                                ins(dp[np + 1][nq][nd * 2], max(ppp.FI, cur_val), cur_val);
                            }
                        }
                        if(nq < m){
                            int nd = (pv < B[nq]);
                            for(auto& ppp : dp[np][nq][sss]){
                                int cur_val = (d == nd ? ppp.SE + 1 : 2);
                                ins(dp[np][nq + 1][nd * 2 + 1], max(ppp.FI, cur_val), cur_val);
                            }
                        }
                    }
                }
            } // end k loop
            FOR(i,p+1,n+1) FOR(j,q+1,m+1) {
                int x = INT_MAX;
                FOR(k,0,4) if (!dp[i][j][k].empty()) x = min(x, dp[i][j][k].begin() -> FI);
                assert(x != INT_MAX);
                x--;
                ans[x]++;
                if (ans[x] == MOD) ans[x] ^= MOD;
            }
        }
    }
    cout << ans << endl;
}