#1249. 不可做题1

内存限制:512 MiB 时间限制:1000 ms 标准输入输出
题目类型:传统 评测方式:文本比较
上传者: 匿名

题目描述

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

Two sequences (x1,x2,,xp)(x_1, x_2, \dots, x_p) and (y1,y2,,yq)(y_1, y_2, \dots, y_q) match iff p=qp = q and xi=xjyi=yjx_i = x_j \Leftrightarrow y_i = y_j for every possible pair 1i,jp1 \leq i, j \leq p.

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

输入格式

The first line contains two integers nn, mm (1n30001 \leq n \leq 3000, 1mmin(5,n)1 \leq m \leq \min(5,n)).

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \leq a_i \leq n).

The third line contains mm integers b1,b2,,bmb_1, b_2,\dots, b_m (1bim1 \leq b_i \leq m).

输出格式

Output one integer: the answer.

样例

10 5
1 5 5 4 1 4 3 3 4 2
3 4 3 2 1
20

4 2
2 2 2 2
2 2
6

数据范围与提示

Good Luck!