CF BUDDY
← Problems·

1366E · Two Arrays

2100 · binary search, brute force, combinatorics

Problem: You are given two arrays a1,a2,,ana_1, a_2, \dots , a_n and b1,b2,,bmb_1, b_2, \dots , b_m. Array bb is sorted in ascending order (bi<bi+1b_i < b_{i + 1} for each ii from 11 to m1m - 1).

You have to divide the array aa into mm consecutive subarrays so that, for each ii from 11 to mm, the minimum on the ii-th subarray is equal to bib_i. Note that each element belongs to exactly one subarray, and they are formed in such a way: the first several elements of aa compose the first subarray, the next several elements of aa compose the second subarray, and so on.

For example, if a=[12,10,20,20,25,30]a = [12, 10, 20, 20, 25, 30] and b=[10,20,30]b = [10, 20, 30] then there are two good partitions of array aa:

  1. [12,10,20],[20,25],[30][12, 10, 20], [20, 25], [30];
  2. [12,10],[20,20,25],[30][12, 10], [20, 20, 25], [30].

You have to calculate the number of ways to divide the array aa. Since the number can be pretty large print it modulo 998244353.

Input Format: The first line contains two integers nn and mm (1n,m21051 \le n, m \le 2 \cdot 10^5) — the length of arrays aa and bb respectively.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots , a_n (1ai1091 \le a_i \le 10^9) — the array aa.

The third line contains mm integers b1,b2,,bmb_1, b_2, \dots , b_m (1bi109;bi<bi+11 \le b_i \le 10^9; b_i < b_{i+1}) — the array bb.

Output Format: In only line print one integer — the number of ways to divide the array aa modulo 998244353.

Sample Cases

Case 1

Input

6 3
12 10 20 20 25 30
10 20 30

Output

2

Case 2

Input

4 2
1 3 3 7
3 7

Output

0

Case 3

Input

8 2
1 2 2 2 2 2 2 2
1 2

Output

7

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback