luogu#P5165. xtq的棋盘

xtq的棋盘

Background

Since second grade, xtq has loved board games.

Problem Description

xtq has a chessboard with 11 row and n+1n+1 columns, numbered from 00 to nn from left to right. At the initial moment, there is a piece at position mm.

xtq will perform random operations over time. Specifically, if in a given second the piece is not at nn, then with probability prbprb he moves the piece one cell to the left, and with probability 1prb1-prb he moves it one cell to the right; otherwise (when the piece is at nn), he will definitely move the piece one cell to the left.

Now xtq wants to ask you: what is the expected number of seconds until the piece can reach 00? Since the answer may be very large, and to avoid unnecessary precision errors, you only need to output the answer modulo 109+710^9+7 (it can be proven that the answer must be a rational number).

Input Format

One line with four positive integers n,m,p,qn, m, p, q.

Here, p,qp, q mean prb=pqprb = \frac{p}{q}.

Output Format

One line with one positive integer, representing the expected number of moves modulo 109+710^9+7.

3 1 1 3
13

Hint

For 20%20\% of the testdata, n4n\le 4, 1pq41\le p\le q\le 4, and it is guaranteed that the answer is an integer before taking modulo.

For 40%40\% of the testdata, n300n\le 300.

For 70%70\% of the testdata, n1000000n\le 1000000.

For 100%100\% of the testdata, 1mn1091\le m\le n\le 10^9, 1pq1091\le p\le q\le 10^9, and p,qp, q are coprime.

In addition, among all testdata points, 30%30\% of the testdata satisfies prb=12prb = \frac{1}{2}.

The modulo of a rational number with respect to a prime pp is defined as follows:

Let the result of ab\frac{a}{b} modulo pp be xx. Then it must satisfy 0x<p0\le x<p and abx(modp)a \equiv bx (mod p).

It is guaranteed that for 100%100\% of the testdata, such an xx satisfying the requirements always exists.

Translated by ChatGPT 5