Problem:
Score: 1000 points

Problem Statement

Find the number of sequences of N non-negative integers A_1, A_2, ..., A_N that satisfy the following conditions:

L \leq A_1 + A_2 + ... + A_N \leq R

When the N elements are sorted in non-increasing order, the M-th and (M+1)-th elements are equal.

Since the answer can be enormous, print it modulo 10^9+7.

Constraints

All values in input are integers.

1 \leq M < N \leq 3 \times 10^5

1 \leq L \leq R \leq 3 \times 10^5

Input

Input is given from Standard Input in the following format:

N M L R

Output

Print the number of sequences of N non-negative integers, modulo 10^9+7.

Sample Input 1

4 2 3 7

Sample Output 1

105

The sequences of non-negative integers that satisfy the conditions are:

\begin{eqnarray} &(1, 1, 1, 0), (1, 1, 1, 1), (2, 1, 1, 0), (2, 1, 1, 1), (2, 2, 2, 0), (2, 2, 2, 1), \\ &(3, 0, 0, 0), (3, 1, 1, 0), (3, 1, 1, 1), (3, 2, 2, 0), (4, 0, 0, 0), (4, 1, 1, 0), \\ &(4, 1, 1, 1), (5, 0, 0, 0), (5, 1, 1, 0), (6, 0, 0, 0), (7, 0, 0, 0)\end{eqnarray}

and their permutations, for a total of 105 sequences.

Sample Input 2

2 1 4 8

Sample Output 2

3

The three sequences that satisfy the conditions are (2, 2), (3, 3), and (4, 4).

Sample Input 3

141592 6535 89793 238462

Sample Output 3

933832916

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include<cstdio>
#include<algorithm>
using namespace std;

typedef long long ll;
const int Maxn=1e6;
const ll Mod=1e9+7;

ll fac[Maxn+5],inv_fac[Maxn+5];
ll QuickPow(ll a,ll k) {
	ll ret=1;
	while(k) {
		if(k&1)ret=(ret*a)%Mod;
		a=(a*a)%Mod;
		k>>=1;
	}
	return ret;
}
void init() {
	fac[0]=1;
	for(int i=1;i<=Maxn;i++)
		fac[i]=fac[i-1]*i%Mod;
	inv_fac[0]=1,inv_fac[Maxn]=QuickPow(fac[Maxn],Mod-2);
	for(int i=Maxn-1;i>=1;i--)
		inv_fac[i]=inv_fac[i+1]*(i+1)%Mod;
}
ll C(int n,int m) {return fac[n]*inv_fac[m]%Mod*inv_fac[n-m]%Mod;}

ll calc(int n,int m,int num,int l,int r) {
	if(num-r*m<0)return 0;
	else num-=(r*m);
	ll ret=0;
	for(int i=0,dir=1;i<=n-m&&num>=0;i++,num-=l,dir*=-1)
		ret=(ret+1LL*dir*C(num+n,n)%Mod*C(n-m,i)%Mod+Mod)%Mod;
	return ret*C(n,m)%Mod;
}
ll Solve(int n,int m,int num) {
	ll ret=C(n+num,n);
	for(int i=num;i>=1;i--) {
		int del=(calc(n,m,num,i,i)-calc(n,m,num,i,i+1)+Mod)%Mod;
		ret=(ret-del+Mod)%Mod;
	}
	return ret;
}

int main() {
	#ifdef LOACL
	freopen("in.txt","r",stdin);
	freopen("out.txt","w",stdout);
	#endif
	int N,M,L,R;
	init();
	scanf("%d %d %d %d",&N,&M,&L,&R);
	printf("%lld\n",(Solve(N,M,R)-Solve(N,M,L-1)+Mod)%Mod);
	return 0;
}