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)

#[allow(dead_code)]
fn is_whitespace(c: &char) -> bool {
    c.is_whitespace()
}

#[allow(dead_code)]
fn is_eol(c: &char) -> bool {
    ['\r', '\n'].contains(c)
}

#[allow(unused_macros)]
macro_rules! input {
    ($var:ident, $($r:tt)*) => {
        input_inner!{$var, $($r)*}
    };

    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        #[allow(unused_mut, unused_variables, bare_trait_objects)]
        let mut next = move |f: &Fn(&char)->bool| -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|f(&c))
                .take_while(|c|!f(&c))
                .collect()
        };
        input_inner!{next, $($r)*};
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; eol ]) => {
        $next(&is_eol).split(' ').map(|x| x.parse::<$t>().unwrap()).collect::<Vec<_>>()
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, parser) => {
        $next
    };

    ($next:expr, $t:ty) => {
        $next(&is_whitespace).parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{BinaryHeap, HashMap, HashSet};

#[allow(dead_code)]
struct Edge {
    from: usize,
    to: usize,
}

#[allow(dead_code)]
type Graph = Vec<Vec<usize>>;
#[allow(dead_code)]
const M: usize = 1_000_000_007;

struct Combination {
    modp: usize,
    mfact: Vec<usize>,
    rfact: Vec<usize>,
}

impl Combination {
    fn new(size: usize, modp: usize) -> Self {
        let mut rfact = vec![0; size + 1];
        let mut mfact = vec![0; size + 1];
        mfact[0] = 1;
        for i in 1..mfact.len() {
            mfact[i] = (mfact[i - 1] * i) % modp;
        }
        rfact[size] = Self::inv(mfact[size], modp);
        for i in (0..size).rev() {
            rfact[i] = rfact[i + 1] * (i + 1) % modp;
        }

        Combination {
            modp: modp,
            mfact: mfact,
            rfact: rfact,
        }
    }

    fn pow(mut x: usize, mut n: usize, modp: usize) -> usize {
        let mut ret = 1;
        while n > 0 {
            if n & 1 > 0 {
                ret *= x;
                ret %= modp;
            }
            x *= x;
            x %= modp;
            n >>= 1;
        }
        ret
    }

    fn inv(x: usize, modp: usize) -> usize {
        Self::pow(x, modp - 2, modp)
    }

    fn C(&self, p: usize, q: usize) -> usize {
        if p < q {
            return 0;
        }
        (((self.mfact[p] * self.rfact[q]) % self.modp) * self.rfact[p - q]) % self.modp
    }
}

fn g(r: usize, c: usize, comb: &Combination) -> usize {
    (comb.C(r + c + 2, c + 1) + M - 1) % M
}

fn main() {
    input! {
        r1: usize1,
        c1: usize1,
        r2: usize,
        c2: usize,
    }
    let comb = Combination::new(2000010, M);

    let mut ans = g(r2, c2, &comb);
    ans += M - g(r2, c1, &comb);
    ans %= M;
    ans += M - g(c2, r1, &comb);
    ans %= M;
    ans += g(r1, c1, &comb);
    ans %= M;

    println!("{}", ans);
}