Under construction

Still being built. Things may change

Third Grader's Task

Time limit1sMemory limit1024 MB

Summary
Count the rearrangements of the symbols in s that are lexicographically less than t, modulo 998244353.
Level

Medium6 of 10

Topics
Combinatorics, Math
Solved
No attempts yet

Problem

Tyler, a little boy, noticed magnets with symbols on the kitchen fridge. The magnets can be lined up to form a string ss.

Tyler likes strings, especially ones that are lexicographically less than string tt. After playing with the magnets, he wonders how many distinct strings can be made by rearranging the letters of ss so that the result is lexicographically less than tt. Tyler is only in third grade and cannot answer this, so count the permutations of the letters of ss that are lexicographically less than tt.

A string xx is lexicographically less than a string yy if one of the following conditions holds.

  • There is a position mm present in both strings such that the strings are equal before the mm-th symbol, and the mm-th symbol of xx is less than the mm-th symbol of yy.
  • xx is a prefix of yy.

Because the answer can be large, print it modulo 998 244 353998\,244\,353.

Input

The first line contains two integers nn and mm (1≤n,m≤200 0001 \le n, m \le 200\,000), the lengths of strings ss and tt.

The second line contains nn integers s1,s2,…,sns_1, s_2, \ldots, s_n (1≤si≤200 0001 \le s_i \le 200\,000), the symbols of ss.

The third line contains mm integers t1,t2,…,tmt_1, t_2, \ldots, t_m (1≤ti≤200 0001 \le t_i \le 200\,000), the symbols of tt.

Output

Print one integer: the number of rearrangements of the letters of ss that are lexicographically less than tt, modulo 998 244 353998\,244\,353.

Hint

In the first sample, the counted strings are [1 2 2] and [2 1 2]. The string [2 2 1] is lexicographically greater than [2 1 2 1], so it is not counted.

In the second sample, every string except [4 3 2 1] is counted, so the answer is 4!−1=234! - 1 = 23.

In the third sample, only [1 1 1 2] is counted.

Examples3

  1. Example 1

    Input
    3 4
    1 2 2
    2 1 2 1
    
    Expected output
    2
    
  2. Example 2

    Input
    4 4
    1 2 3 4
    4 3 2 1
    
    Expected output
    23
    
  3. Example 3

    Input
    4 3
    1 1 1 2
    1 1 2
    
    Expected output
    1