Problem:
Score : 600 points

Problem Statement

Snuke is standing on a two-dimensional plane. In one operation, he can move by 1 in the positive x-direction, or move by 1 in the positive y-direction.

Let us define a function f(r, c) as follows:

f(r,c) := (The number of paths from the point (0, 0) to the point (r, c) that Snuke can trace by repeating the operation above)

Given are integers r_1, r_2, c_1, and c_2.
Find the sum of f(i, j) over all pair of integers (i, j) such that r_1 ≤ i ≤ r_2 and c_1 ≤ j ≤ c_2, and compute this value modulo (10^9+7).

Constraints

1 ≤ r_1 ≤ r_2 ≤ 10^6

1 ≤ c_1 ≤ c_2 ≤ 10^6

All values in input are integers.

Input

Input is given from Standard Input in the following format:

r_1 c_1 r_2 c_2

Output

Print the sum of f(i, j) modulo (10^9+7).

Sample Input 1

1 1 2 2

Sample Output 1

14

For example, there are two paths from the point (0, 0) to the point (1, 1): (0,0) → (0,1) → (1,1) and (0,0) → (1,0) → (1,1), so f(1,1)=2.

Similarly, f(1,2)=3, f(2,1)=3, and f(2,2)=6. Thus, the sum is 14.

Sample Input 2

314 159 2653 589

Sample Output 2

602215194

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

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use proconio::input;

pub struct ModCombin {
    factv: Vec<usize>,
    finvv: Vec<usize>,
    modp: usize,
}

impl ModCombin {
    pub fn new(max: usize, modp: usize) -> Self {
        let mut factv = vec![0usize; max + 1];
        let mut finvv = vec![0usize; max + 1];
        factv[0] = 1;
        for i in 1..=max {
            factv[i] = (factv[i - 1] * i) % modp;
        }
        finvv[max] = Self::inv(factv[max], modp);
        for i in (1..=max).rev() {
            finvv[i - 1] = (finvv[i] * i) % modp;
        }
        Self { factv, finvv, modp }
    }

    pub fn combination(&self, n: usize, r: usize) -> usize {
        if n < r {
            return 0;
        }
        ((((self.factv[n] * self.finvv[r]) % self.modp) * self.finvv[n - r]) % self.modp)
    }

    // private
    fn inv(n: usize, modp: usize) -> usize {
        Self::pow(n, (modp - 2) as u32, modp)
    }

    // private
    fn pow(n: usize, mut x: u32, modp: usize) -> usize {
        let mut b = n;
        let mut a = 1usize;
        while x > 1 {
            if x & 1 == 1 {
                a = (a * b) % modp;
            }
            x = x / 2;
            b = (b * b) % modp;
        }
        if x == 1 {
            a = (a * b) % modp;
        }
        a
    }
}

fn g(mc: &ModCombin, r: usize, c: usize) -> usize {
    return (mc.modp + mc.combination((r + 1) + (c + 1), r + 1) - 1) % mc.modp;
}

fn main() {
    input! {
        r1: usize,
        c1: usize,
        r2: usize,
        c2: usize
    };
    let modp = 1_000_000_007;
    let mc = ModCombin::new(2_000_005, modp);
    let ans = (modp + modp + g(&mc, r2, c2) - g(&mc, r1 - 1, c2) - g(&mc, r2, c1 - 1)
        + g(&mc, r1 - 1, c1 - 1))
        % modp;
    println!("{}", ans);
}
*/

fn main() {
    let exe = "/tmp/bin01CD0CF7";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";