Under construction

Still being built. Things may change

Evil Subsequence

Time limit3sMemory limit512 MB

Summary
Count the subsequences of a whose equal-and-different pattern matches b exactly, so positions i and j hold equal values in the subsequence precisely when b_i equals b_j.
Level

Medium6 of 10

Topics
Dynamic programming, Hash map
Solved
No attempts yet

Problem

It's just a problem to waste your time.

You are given two sequences a1,a2,…,ana_1, a_2, \ldots, a_n and b1,b2,…,bmb_1, b_2, \ldots, b_m.

Two sequences (x1,x2,…,xp)(x_1, x_2, \ldots, x_p) and (y1,y2,…,yq)(y_1, y_2, \ldots, y_q) match if and only if p=qp = q and, for every pair 1≤i,j≤p1 \le i, j \le p, xi=xjx_i = x_j exactly when yi=yjy_i = y_j.

Output the number of subsequences of a1,a2,…,ana_1, a_2, \ldots, a_n that match b1,b2,…,bmb_1, b_2, \ldots, b_m.

Input

The first line contains two integers nn and mm (1≤n≤30001 \le n \le 3000, 1≤m≤min⁡(5,n)1 \le m \le \min(5, n)).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤n1 \le a_i \le n).

The third line contains mm integers b1,b2,…,bmb_1, b_2, \ldots, b_m (1≤bi≤m1 \le b_i \le m).

Output

Output one integer: the answer.

Examples2

  1. Example 1

    Input
    10 5
    1 5 5 4 1 4 3 3 4 2
    3 4 3 2 1
    
    Expected output
    20
    
  2. Example 2

    Input
    4 2
    2 2 2 2
    2 2
    
    Expected output
    6