#P16822. [蓝桥杯 2026 国 Python B] 零段积分

[蓝桥杯 2026 国 Python B] 零段积分

Problem Description

Xiao Lan is doing a random string experiment. She has a sequence of length NN, and initially all positions are 00. After the experiment starts, each position in the sequence independently turns into 11 with probability PQ\frac{P}{Q}, and all other positions remain 00.

After the experiment ends, all consecutive 00's are separated by 11's into several segments.

Let the lengths of the non-empty consecutive zero segments from left to right be l1,l2,…,lkl_1, l_2, \dots, l_k.

Xiao Lan defines the value of the sequence as

S=∑i=1k−1li×li+1S = \sum_{i=1}^{k-1} l_i \times l_{i+1}

That is, the value is the sum of the products of the lengths of all adjacent zero segments. If the number of zero segments is less than 22, then the value is 00.

Now, please compute the expected value of SS, and output the result modulo 109+710^9 + 7.

Input Format

Input one line containing three integers N,P,QN, P, Q, representing the length of the sequence, and the numerator and denominator of the probability that each position turns into 11.

It is guaranteed that 0≤P≤Q0 \le P \le Q, Q>0Q > 0, and gcd⁡(P,Q)=1\gcd(P, Q) = 1.

Output Format

Output one line with one integer, representing the expected value of SS modulo 109+710^9 + 7.

If the expected value is a fraction ab\frac{a}{b}, output a×b−1 mod (109+7)a \times b^{-1} \bmod (10^9 + 7), where b−1b^{-1} denotes the multiplicative inverse of bb modulo 109+710^9 + 7.

4 1 2
937500007

Hint

Sample Explanation

When N=4N=4 and each position turns into 11 with probability 12\frac{1}{2}, all 242^4 states occur with equal probability, but only the following states have a non-zero value:

State Zero-segment lengths Value
00100010 2,12, 1 22
01000100 1,21, 2
01010101 1,11, 1 11
01100110
10101010

Therefore, E[S]=2+2+1+1+116=716E[S] = \frac{2+2+1+1+1}{16} = \frac{7}{16}. Modulo 109+710^9 + 7, 716\frac{7}{16} equals 937500007937500007.

Constraints and Conventions for Test Cases

For 30%30\% of the test cases, 1≤N≤201 \le N \le 20.

For all test cases, 1≤N≤1061 \le N \le 10^6, 0≤P≤Q≤1090 \le P \le Q \le 10^9, and gcd⁡(P,Q)=1\gcd(P, Q) = 1.

Translated by ChatGPT 5