#P5510. Effective Attack Calculation

    ID: 18742 Type: Default 1000ms 256MiB

Effective Attack Calculation

Effective Attack Calculation

The dark forces have arranged their crystals in a row – and there are many of them. The crystals are divided into \(n\) groups: in the \(i\)-th group there are \(a_i\) crystals, and every crystal has defense power \(n\cdot a_i\). Similarly, Steve's weapons are arranged in a row and divided into \(n\) groups: in the \(i\)-th group there are \(b_i\) weapons, and each weapon has attack power \(n\cdot b_i\).

In each round of attack, the dark forces choose a crystal and Steve chooses a weapon. An attack is considered effective if the weapon's attack power is greater than the crystal's defense power. In other words, if a crystal is chosen from group \(i\) and a weapon is chosen from group \(j\), the attack is effective if \[ n\cdot b_j > n\cdot a_i \quad \Longleftrightarrow \quad b_j > a_i. \]

Steve wants to know:

  1. For all possible selections, how many effective attack configurations are there? (Note that the number of ways for an effective attack is the sum over all valid pairs of \(a_i\) and \(b_j\) multiplied by the number of crystals and weapons in those groups, that is \(a_i \times b_j\) for every pair with \(b_j > a_i\).)
  2. If it is known that the chosen crystal's defense power lies between the defense power of group \(x\) and group \(y\) (i.e. between \(\min(n\cdot a_x,\, n\cdot a_y)\) and \(\max(n\cdot a_x,\, n\cdot a_y)\)) and the chosen weapon's attack power lies between the attack power of group \(z\) and group \(u\) (i.e. between \(\min(n\cdot b_z,\, n\cdot b_u)\) and \(\max(n\cdot b_z,\, n\cdot b_u)\)), how many effective attack configurations exist under these restrictions?

Two selections are considered different if either a different crystal or a different weapon is chosen (even if they belong to the same group).

Since the battle is urgent and the numbers may be huge, output your answers modulo \(998244353\).

inputFormat

The input consists of four lines:

  • The first line contains an integer \(n\) indicating the number of groups for both crystals and weapons.
  • The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) representing the number of crystals in each group.
  • The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) representing the number of weapons in each group.
  • The fourth line contains four integers \(x\), \(y\), \(z\), \(u\). Here, the allowed crystal defense power is between that of group \(x\) and group \(y\) (inclusive), and the allowed weapon attack power is between that of group \(z\) and group \(u\) (inclusive).

Remember that a crystal in group \(i\) has defense power \(n\cdot a_i\) and a weapon in group \(j\) has attack power \(n\cdot b_j\); however, since the inequality \(n\cdot b_j > n\cdot a_i\) is equivalent to \(b_j > a_i\), you only need to compare \(a_i\) and \(b_j\).

outputFormat

Output two integers separated by a space:

  1. The first integer is the total number of effective attack configurations over all choices.
  2. The second integer is the number of effective attack configurations when the crystal is chosen from a group with \(a_i\) between \(\min(a_x, a_y)\) and \(\max(a_x, a_y)\) and the weapon is chosen from a group with \(b_j\) between \(\min(b_z, b_u)\) and \(\max(b_z, b_u)\).

Both answers must be given modulo \(998244353\).

sample

3
1 2 3
2 3 4
1 3 1 2
35 11