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
import sys

MOD = 1000000007


def intervals(seq):
    for i in range(len(seq)):
        for j in range(i, len(seq)):
            yield seq[i : j + 1]


def convolutions(seq_a, seq_b):
    if not seq_a:
        yield seq_b
        return

    if not seq_b:
        yield seq_a
        return

    if len(seq_a) > len(seq_b):
        seq_a, seq_b = seq_b, seq_a

    for pos in range(len(seq_b)):
        for conv in convolutions(seq_a[1:], seq_b[pos:]):
            yield seq_b[:pos] + [seq_a[0]] + conv


def stability(seq):
    result = 0
    inc = 0
    dec = 0
    for i, v in enumerate(seq):
        if i:
            if seq[i - 1] >= v:
                inc = 0

            if seq[i - 1] <= v:
                dec = 0

        inc += 1
        dec += 1
        result = max(result, inc, dec)

    return result


def min_convolution_stability(seq_a, seq_b):
    min_stability = len(seq_a) + len(seq_b)
    for seq in convolutions(seq_a, seq_b):
        min_stability = min(min_stability, stability(seq))

    return min_stability


if __name__ == "__main__":
    data = [int(x) for x in sys.stdin.read().split()]
    length_a = data[0]
    length_b = data[1]

    seq_a = data[2 : 2 + length_a]
    seq_b = data[2 + length_a : 2 + length_a + length_b]

    result = [0] * (length_a + length_b)
    for int_a in intervals(seq_a):
        for int_b in intervals(seq_b):
            sta = min_convolution_stability(int_a, int_b)
            result[sta - 1] = (result[sta - 1] + 1) % MOD

    print(" ".join(map(str, result)))