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 Rust (1.15.1) (judge verdict: Accepted)

// ---------- begin ModInt ----------
//https://github.com/kenkoooo/competitive-programming-rs/blob/master/src/math/mod_int.rs
//を参考にしています
#[allow(dead_code)]
mod modint {
    pub const MOD: u32 = 1_000_000_007;
    use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
    #[derive(Clone, Copy)]
    pub struct ModInt<T: Copy + Clone>(pub T);
    type Num = u32;
    impl Add<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn add(self, other: ModInt<Num>) -> ModInt<Num> {
            let mut d = self.0 + other.0;
            if d >= MOD {
                d -= MOD;
            }
            ModInt(d)
        }
    }
    impl AddAssign<ModInt<Num>> for ModInt<Num> {
        fn add_assign(&mut self, other: ModInt<Num>) {
            *self = *self + other;
        }
    }
    impl Sub<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn sub(self, other: ModInt<Num>) -> ModInt<Num> {
            let mut d = self.0 + MOD - other.0;
            if d >= MOD {
                d -= MOD;
            }
            ModInt(d)
        }
    }
    impl SubAssign<ModInt<Num>> for ModInt<Num> {
        fn sub_assign(&mut self, other: ModInt<Num>) {
            *self = *self - other;
        }
    }
    impl Mul<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn mul(self, other: ModInt<Num>) -> ModInt<Num> {
            ModInt(((self.0 as u64) * (other.0 as u64) % (MOD as u64)) as u32)
        }
    }
    impl MulAssign<ModInt<Num>> for ModInt<Num> {
        fn mul_assign(&mut self, other: ModInt<Num>) {
            *self = *self * other;
        }
    }
    impl Div<ModInt<Num>> for ModInt<Num> {
        type Output = ModInt<Num>;
        fn div(self, other: ModInt<Num>) -> ModInt<Num> {
            self * other.pow(MOD - 2)
        }
    }
    impl DivAssign<ModInt<Num>> for ModInt<Num> {
        fn div_assign(&mut self, other: ModInt<Num>) {
            *self = *self / other;
        }
    }
    impl Neg for ModInt<Num> {
        type Output = ModInt<Num>;
        fn neg(self) -> ModInt<Num> {
            ModInt(if self.0 == 0 { 0 } else { MOD - self.0 })
        }
    }
    impl ModInt<Num> {
        pub fn new(v: u32) -> ModInt<Num> {
            ModInt(v % MOD)
        }
        pub fn pow(self, mut n: u32) -> ModInt<Num> {
            let mut t = ModInt::new(1);
            let mut s = self;
            while n > 0 {
                if (n & 1) == 1 {
                    t *= s;
                }
                s *= s;
                n >>= 1;
            }
            t
        }
    }
    pub struct Precalc {
        n: usize,
        inv: Vec<ModInt<Num>>,
        fact: Vec<ModInt<Num>>,
        ifact: Vec<ModInt<Num>>,
    }
    impl Precalc {
        pub fn new(n: usize) -> Precalc {
            let mut inv = vec![ModInt(1); n + 1];
            let mut fact = vec![ModInt(1); n + 1];
            let mut ifact = vec![ModInt(1); n + 1];
            for i in 1..(n + 1) {
                if i >= 2 {
                    inv[i] = -inv[(MOD as usize) % i] * ModInt(MOD / (i as u32));
                }
                fact[i] = ModInt(i as u32) * fact[i - 1];
                ifact[i] = inv[i] * ifact[i - 1];
            }
            Precalc {
                n: n,
                inv: inv,
                fact: fact,
                ifact: ifact,
            }
        }
        pub fn fact(&self, n: usize) -> ModInt<Num> {
            self.fact[n]
        }
        pub fn inv(&self, x: usize) -> ModInt<Num> {
            self.inv[x]
        }
        pub fn ifact(&self, x: usize) -> ModInt<Num> {
            self.ifact[x]
        }
        pub fn comb(&self, n: usize, k: usize) -> ModInt<Num> {
            if !(k <= n) {
                return ModInt(0);
            }
            self.fact[n] * self.ifact[k] * self.ifact[n - k]
        }
    }
    use std;
    impl std::fmt::Display for ModInt<Num> {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            write!(f, "{}", self.0)
        }
    }
}
// ---------- end ModInt ----------

use modint::*;

fn calc(r: usize, n: usize, m: usize, pc: &Precalc) -> ModInt<u32> {
    let mut sum = ModInt(0);
    for k in 1..(r / m + 1) {
        let r = r - k * m;
        let mut sign = ModInt(1);
        let mut i = 0;
        while i <= n - m && k * i <= r {
            let way = if m <= r - k * i {
                pc.comb(r - k * i + n, n) - pc.comb(r - k * i - m + n, n)
            } else {
                pc.comb(r - k * i + n, n)
            };
            sum += sign * pc.comb(n - m, i) * way;
            i += 1;
            sign = -sign;
        }
    }
    pc.comb(r + n, n) - sum * pc.comb(n, m)
}

fn run() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    let mut it = s.trim().split_whitespace();
    let n: usize = it.next().unwrap().parse().unwrap();
    let m: usize = it.next().unwrap().parse().unwrap();
    let l: usize = it.next().unwrap().parse().unwrap();
    let r: usize = it.next().unwrap().parse().unwrap();
    let pc = Precalc::new(r + n);
    let ans = calc(r, n, m, &pc) - calc(l - 1, n, m, &pc);
    println!("{}", ans);
}

fn main() {
    run();
}