# 区間DPで、区間ごとに最後に残すことができる石を考える n, k = map(int, input().split()) a_list = list(map(int, input().split())) c_list = list(map(int, input().split())) sum_a = sum(a_list) dp = [[[0] * 50 for _1 in range(2 * n)] for _2 in range(2 * n)] # 初期化 for i in range(2 * n): c = c_list[i % n] - 1 dp[i][i][c] = 1 # 区間DP for d in range(1, n): for i in range(2 * n - d): for j in range(d): dp[i][i + d][c] = dp[i][i + j][c] * dp[i + j + 1][i + d][c] print(0)