-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubseq.py
More file actions
51 lines (45 loc) · 1.17 KB
/
subseq.py
File metadata and controls
51 lines (45 loc) · 1.17 KB
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
MOD = 998244353
def _find_range(a: list, b: list):
if len(b) == 1:
return int(b[0] == min(a))
for x in range(len(a) - 1, len(a) - len(b) - 2, -1):
if a[x] < b[-1]:
return 0
if b[-1] == a[x]:
j = x
break
else:
return 0
bi = len(b) - 1
count = 0
mult = 1
while j >= 0 and bi > 0:
if a[j] < b[bi]:
mult = count * mult
count = 0
bi -= 1
while j >= 0:
if a[j] < b[bi]:
return 0
if a[j] == b[bi]:
break
j -= 1
else:
return 0
j -= 1
count += 1
mult = mult * count
if bi > 1:
return 0
if min(a[: j + 2]) == b[0]:
return mult % MOD
else:
return 0
def count_permutations(a: list, b: list):
return _find_range(a, b)
if __name__ == "__main__":
(n, m) = tuple([int(x) for x in input().rstrip().split()])
a = list([int(x) for x in input().rstrip().split()])
b = list([int(x) for x in input().rstrip().split()])
result = count_permutations(a, b)
print(result)