#P10185. [YDOI R1] Necklace

[YDOI R1] Necklace

Background

hdkk is making a necklace.

Problem Description

hdkk has nn colors of beads. For the ii-th color, there are aia_i beads. He can choose any number of beads and string them into a necklace.

Each color has a beauty value viv_i. hdkk defines a “beauty score” for a necklace: if the ii-th color appears cntcnt times in the necklace and cnt1cnt \ge 1, then the beauty score of this necklace increases by (vi)cnt(v_i)^{cnt}.

Now he wants to know the sum of beauty scores over all different necklaces. Please compute the answer modulo 109+710^9+7.

Two necklaces are considered different if and only if there exists a bead that appears in one necklace but does not appear in the other.

Note that every bead is distinct, even if they have the same color.

Input Format

Line 11 contains one positive integer nn.

Line 22 contains nn integers, where the ii-th number is aia_i.

Line 33 contains nn integers, where the ii-th number is viv_i.

Output Format

Output one integer: the sum of beauty scores of all different necklaces modulo 109+710^9+7.

2
1 2
2 3 
38
2
18 2
9 1
786624

Hint

Sample Explanation #1

Color 11: {1}\left\{1\right\}, color 22: {2,3}\left\{2,3\right\}.

There are 77 different necklaces in total: $\left \{1 \right \},\left \{2\right \},\left \{3\right \},\left \{1,2 \right \},\left \{1,3 \right \},\left \{2,3 \right \},\left \{1,2,3 \right \}$. The total beauty score is 2+3+3+(2+3)+(2+3)+32+(2+32)=382+3+3+(2+3)+(2+3)+3^2+(2+3^2)=38.

This problem uses bundled testdata.

Subtask ID nn\le aia_i\le Points
11 44 55 1515
22 10310^3 2525
33 2×1052\times10^5 10910^9 6060

For all testdata, it is guaranteed that 1n2×1051\le n\le2\times10^5, 1ai1091\le a_i\le10^9, and 1vi1091\le v_i\le10^9.

Translated by ChatGPT 5