Problem:
Score : 400 points

Problem Statement

We have N+1 integers: 10^{100}, 10^{100}+1, ..., 10^{100}+N.

We will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (10^9+7).

Constraints

1 \leq N \leq 2\times 10^5

1 \leq K \leq N+1

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K

Output

Print the number of possible values of the sum, modulo (10^9+7).

Sample Input 1

3 2

Sample Output 1

10

The sum can take 10 values, as follows:

(10^{100})+(10^{100}+1)=2\times 10^{100}+1

(10^{100})+(10^{100}+2)=2\times 10^{100}+2

(10^{100})+(10^{100}+3)=(10^{100}+1)+(10^{100}+2)=2\times 10^{100}+3

(10^{100}+1)+(10^{100}+3)=2\times 10^{100}+4

(10^{100}+2)+(10^{100}+3)=2\times 10^{100}+5

(10^{100})+(10^{100}+1)+(10^{100}+2)=3\times 10^{100}+3

(10^{100})+(10^{100}+1)+(10^{100}+3)=3\times 10^{100}+4

(10^{100})+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+5

(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=3\times 10^{100}+6

(10^{100})+(10^{100}+1)+(10^{100}+2)+(10^{100}+3)=4\times 10^{100}+6

Sample Input 2

200000 200001

Sample Output 2

1

We must choose all of the integers, so the sum can take just 1 value.

Sample Input 3

141421 35623

Sample Output 3

220280457

Solution in Rust (1.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(unused_macros)]
#![allow(unused_mut)]
use num::integer::gcd;
use proconio::fastout;
use proconio::input;
use proconio::marker::{Bytes, Chars};
use std::f64::consts::PI;
use std::mem::swap;

type Pi = (i32, i32);

fn pri<T: std::fmt::Display>(x: T) {
    println!("{}", x);
}

#[fastout]
fn main() {
    input! {
        n: u32, k:u32,
    }
    let mut ans = Mint(0);
    let mut sum = vec![Mint(0)];
    for i in 0..n + 1 {
        sum.push(Mint(i) * (Mint(i + 1)) * Mint(2).inv());
    }
    let n = n as usize;
    let k = k as usize;
    for i in k..n + 2 {
        ans += sum[n + 1] - sum[n + 1 - i] - sum[i] + Mint(1);
    }
    pri(ans);
}

// Modint
const MOD: u32 = 1_000_000_007;
#[derive(Clone, Copy)]
struct Mint(u32);
impl std::ops::Add for Mint {
    type Output = Mint;
    fn add(self, x: Mint) -> Mint {
        let res = self.0 + x.0;
        Mint(if res >= MOD { res - MOD } else { res })
    }
}
impl std::ops::AddAssign for Mint {
    fn add_assign(&mut self, x: Mint) {
        *self = *self + x;
    }
}
impl std::ops::Sub for Mint {
    type Output = Mint;
    fn sub(self, x: Mint) -> Mint {
        let res = self.0 + MOD - x.0;
        Mint(if res >= MOD { res - MOD } else { res })
    }
}
impl std::ops::SubAssign for Mint {
    fn sub_assign(&mut self, x: Mint) {
        *self = *self - x;
    }
}
impl std::ops::Mul for Mint {
    type Output = Mint;
    fn mul(self, x: Mint) -> Mint {
        Mint((self.0 as u64 * x.0 as u64 % MOD as u64) as u32)
    }
}
impl std::ops::MulAssign for Mint {
    fn mul_assign(&mut self, x: Mint) {
        *self = *self * x;
    }
}
impl std::ops::Div for Mint {
    type Output = Mint;
    fn div(self, x: Mint) -> Mint {
        let mut a = x.0 as i64; //solves au + mv = 1 and u = a^-1
        let mut m = MOD as i64;
        let mut u = 1i64;
        let mut v = 0i64;
        while m != 0 {
            let t = a / m;
            a -= t * m;
            swap(&mut a, &mut m);
            u -= t * v;
            swap(&mut u, &mut v);
        }
        u %= m;
        if u < 0 {
            u += m;
        }
        self * Mint(u as u32)
    }
}
impl std::ops::DivAssign for Mint {
    fn div_assign(&mut self, x: Mint) {
        *self = *self / x;
    }
}
impl std::fmt::Display for Mint {
    fn fmt<'a>(&self, f: &mut std::fmt::Formatter<'a>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl Mint {
    pub fn pow(self, mut n: u32) -> Mint {
        let mut res = Mint(1);
        let mut r = self;
        while n > 0 {
            if n & 1 == 1 {
                res *= r;
            }
            r *= r;
            n >>= 1;
        }
        res
    }
    pub fn inv(self) -> Mint {
        assert!(self.0 != 0);
        self.pow(MOD - 2)
    }
}
fn facinit(n: u32) -> Vec<Mint> {
    let mut res = Vec::new();
    let mut k = Mint(1);
    res.push(k);
    for i in 1..n + 1 {
        k *= Mint(i);
        res.push(k);
    }
    res
}