/* * ******************************************************************************************** * AUTHOR : Vijju123 * * Language: C++14 * * Purpose: - * * IDE used: Codechef IDE. * ******************************************************************************************** * Comments will be included in practice problems if it helps ^^ */ #include #include using namespace std; long long m=1000000007; long long n,a,b,k; int power(long long a,long long m1) { if(m1==0)return 1; else if(m1==1)return a; else if(m1==2)return (1LL*a*a)%m; else if(m1&1)return (1LL*a*power(power(a,m1/2),2))%m; else return power(power(a,m1/2),2); } int main() { // your code goes here #ifdef JUDGE freopen("input.txt", "rt", stdin); freopen("output.txt", "wt", stdout); #endif ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin>>n>>k>>a>>b; long long dp[10000001]={0}; dp[1]=k%m; dp[2]=(1LL*k*a+1LL*b*((1LL*k*k)%m))%m; long long c1=(a+2LL*k*b)%m,c2=(1LL*a*a)%m; long long fac[10000001]={1,1},inv[10000001]={0}; for(int i=2;i<=n;i++) { fac[i]=(fac[i-1]*i)%m; } long long invFac=power(fac[n],m-2);//1/(n+1)=n!/(n+1)!. We use this for calculation of inverse in linear time. //This allows us to use invFactorial[i]=(i+1)*invFac[till (i+1)] (as 1/i=i!*1/(i+1)! for(int i=n;i>=1;--i) { inv[i]=(fac[i-1]*invFac)%m; invFac=(invFac*i)%m; } for(int i=3;i<=n;++i) { dp[i]=(inv[i]*((m+((c1*(2*i-3))%m*dp[i-1])%m-((c2*(i-3))%m*dp[i-2])%m )%m ))%m; } int q,l,r; cin>>q; for(int i=1;i<=n;i++) dp[i]=(dp[i-1]+(dp[i]*dp[i])%m)%m;//prefix sum. Note the 1-based indexing while(q--) { cin>>l>>r; cout<<(m+dp[r]-dp[l-1])%m<