PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000065. Sắp xếp hai hàng que diêm (Matchstick Arrangement)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Sắp xếp hai hàng que diêm (Matchstick Arrangement)

Matchstick Arrangement

Source: Luogu

Version: Phuoc Hung OJ Extended

Problem Statement

There are two rows of matchsticks, each containing nn sticks. Stick ii in the first row has height aia_i and stick ii in the second row has height bib_i. All heights within each row are pairwise distinct.

The distance between the rows is

D=∑i=1n(ai−bi)2.D=\sum_{i=1}^{n}(a_i-b_i)^2.

You may swap two adjacent sticks within the same row, at a cost of one operation. Swaps may be performed in either row.

First minimize the distance DD. Among all arrangements attaining its minimum, determine the minimum total number of adjacent swaps across both rows. Output this number, not the distance.

Input

The first line contains nn.

The second line contains nn integers a1,…,ana_1,\ldots,a_n, the first row heights from left to right.

The third line contains nn integers b1,…,bnb_1,\ldots,b_n, the second row heights.

Output

Print one integer: the minimum number of adjacent swaps, modulo 108−3=99 999 99710^8-3=99\,999\,997.

Subtasks

  • Subtask 1 (10 points): 1≤n≤101\le n\le 10; 0≤ai,bi≤21474836470\le a_i,b_i\le 2147483647 for each stick; all aia_i are distinct and all bib_i are distinct.

  • Subtask 2 (20 points): 1≤n≤1001\le n\le 100; 0≤ai,bi≤21474836470\le a_i,b_i\le 2147483647 for each stick; all aia_i are distinct and all bib_i are distinct.

  • Subtask 3 (30 points): 1≤n≤10001\le n\le 1000; 0≤ai,bi≤21474836470\le a_i,b_i\le 2147483647 for each stick; all aia_i are distinct and all bib_i are distinct.

  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000; 0≤ai,bi≤21474836470\le a_i,b_i\le 2147483647 for each stick; all aia_i are distinct and all bib_i are distinct.

Examples

Input

4
2 3 1 4
3 2 1 4

Output

1

Explanation

Both rows contain the heights {1,2,3,4}\{1,2,3,4\}, so their minimum distance is D=0D=0, achieved when the height sequences match.

Initially the rows are [2,3,1,4][2,3,1,4] and [3,2,1,4][3,2,1,4]. Swapping the first two sticks of the first row makes both rows [3,2,1,4][3,2,1,4], attaining D=0D=0 in one operation. Since the original rows differ, zero swaps cannot achieve D=0D=0. Hence the minimum is 11, and the modulo result is 1.