复习概率dp,想到这题就拿出来练一下。

D. New Year and Arbitrary Arrangement

time limit per test

memory limit per test

input

output


kpa and pb.

pa / (pa + pb), add 'a' to the end of the sequence. Otherwise (with probability pb / (pa + pb)), add 'b' to the end of the sequence.

k subsequences that form 'ab'. Determine the expected number of times 'ab' is a subsequence in the resulting sequence. It can be shown that this can be represented by P / Q, where P and Q are coprime integers, and 

Good Bye 2017-D. New Year and Arbitrary Arrangement_概率dp

. Print the value of 

Good Bye 2017-D. New Year and Arbitrary Arrangement_子序列_02

.

Input


k, pa, pb (1 ≤ k ≤ 1 000, 1 ≤ pa, pb).


Output


Print a single integer, the answer to the problem.


Examples


input


1 1 1


output


2


input


3 1 4


output


370000006


Note


ab' at least once. For instance, we get the sequence 'ab' with probability 1/4, 'bbab' with probability 1/16, and 'aab' with probability 1/8. Note, it's impossible for us to end with a sequence like 'aabab', since we would have stopped our algorithm once we had the prefix 'aab'.

ab' will occur across all valid sequences is 2.

For the second sample, the answer is equal to 

Good Bye 2017-D. New Year and Arbitrary Arrangement_概率dp_03

.

       其实概率dp也是dp中比较难的,dp不是求最值的吗?怎么用它来算概率?会让人摸不着头脑。它的状态转移方程

通常冗杂繁复,一看就让人头大。 

       当我看到这道题,第一反应概率dp,然后不会!看题解发现照着它写能写出来,但自己推又一脸懵逼了,所以我

是真的菜。

       好了,那我们看看这道题。

       定义dp[i][j]表示当前串中出现了 i个子序列'a',j个子序列'ab'时,再继续做下去,最后停止时的子序列'ab'期望出现

概率。我们向dp[i][j]后附加一个a时,转移到的状态应该是dp[i+1][j],附加一个b时,转移到的状态应该是dp[i][i+j]。

因此我们的状态转移方程为:dp[i][j]=pa/(pa+pb)*dp[i+1][j]+pb/(pa+pb)*dp[i][i+j]

我们的初始状态是dp[i][j] = j 这里i随意,j>=k。我们的末状态是dp[0][0],即对应着一开始的空串。

但是,上面的状态定义其实有两个小问题。

       1. 我们刚刚说了,初始状态dp[i][j]=j 这里i随意,也就事实上也就意味着字符a可以无限的附加下去。因此我们做

一个转化,变成dp[i][j]= j + i +pa/pb 这里i+j>=k。

       我们来看这个式子,按照定义dp[i][j]停止后ab出现的期望,因为i+j>=k,因此只要出现一个b即会停止,因此我们

这里采用一个几何分布的期望,pa/pb代表着出现第一次出现b的时候,a的期望出现次数,然后再加上本来就有i个a,

因此最后的ab的数量就是 i+j+pa/pb。

       2. 由于b可以在第一个a出现之前无穷的出现,因此我们把最终求解的状态设置为 dp[1][0]。

       所以一开始就把 pa处理成 pa*(pa+pb)^-1 , 把pb处理成 pb*(pa+pb)^-1 以后直接这样计算即可。

Code:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int f[1001][1001];
ll inv(ll a)
{
ll s=1;
for(int n=mod-2;n;n>>=1)
{
if(n&1)s=s*a%mod;
a=a*a%mod;
}
return s;
}
int main()
{
int n,a,b;
cin>>n>>a>>b;
ll u=a*inv(a+b)%mod;
ll v=(1-u+mod)%mod;
ll c=u*inv(v)%mod;
for(int i=n;i>0;i--)
for(int j=n-1;j>-1;j--)
f[i][j]=(j+i>=n?j+i+c:u*f[i+1][j]+v*f[i][j+i])%mod;
printf("%d\n",f[1][0]);
}