Problem:
Score : 900 points

Problem Statement

There are N robots and M exits on a number line.
The N + M coordinates of these are all integers and all distinct.
For each i (1 \leq i \leq N), the coordinate of the i-th robot from the left is x_i.
Also, for each j (1 \leq j \leq M), the coordinate of the j-th exit from the left is y_j.

Snuke can repeatedly perform the following two kinds of operations in any order to move all the robots simultaneously:

Increment the coordinates of all the robots on the number line by 1.

Decrement the coordinates of all the robots on the number line by 1.

Each robot will disappear from the number line when its position coincides with that of an exit, going through that exit.
Snuke will continue performing operations until all the robots disappear.

When all the robots disappear, how many combinations of exits can be used by the robots?
Find the count modulo 10^9 + 7.
Here, two combinations of exits are considered different when there is a robot that used different exits in those two combinations.

Constraints

1 \leq N, M \leq 10^5

1 \leq x_1 < x_2 < ... < x_N \leq 10^9

1 \leq y_1 < y_2 < ... < y_M \leq 10^9

All given coordinates are integers.

All given coordinates are distinct.

Input

Input is given from Standard Input in the following format:

N M
x_1 x_2 ... x_N
y_1 y_2 ... y_M

Output

Print the number of the combinations of exits that can be used by the robots when all the robots disappear, modulo 10^9 + 7.

Sample Input 1

2 2
2 3
1 4

Sample Output 1

3

The i-th robot from the left will be called Robot i, and the j-th exit from the left will be called Exit j.
There are three possible combinations of exits (the exit used by Robot 1, the exit used by Robot 2) as follows:

(Exit 1, Exit 1)

(Exit 1, Exit 2)

(Exit 2, Exit 2)

Sample Input 2

3 4
2 5 10
1 3 7 13

Sample Output 2

8

The exit for each robot can be chosen independently, so there are 2^3 = 8 possible combinations of exits.

Sample Input 3

4 1
1 2 4 5
3

Sample Output 3

1

Every robot uses Exit 1.

Sample Input 4

4 5
2 5 7 11
1 3 6 9 13

Sample Output 4

6

Sample Input 5

10 10
4 13 15 18 19 20 21 22 25 27
1 5 11 12 14 16 23 26 29 30

Sample Output 5

22

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

use proconio::input;

trait ModValue:
    std::ops::Add<Output = Self>
    + std::ops::Sub<Output = Self>
    + std::ops::Mul<Output = Self>
    + std::ops::Div<Output = Self>
    + std::ops::Rem<Output = Self>
    + std::ops::AddAssign
    + std::ops::SubAssign
    + std::ops::MulAssign
    + std::ops::DivAssign
    + std::ops::RemAssign
    + std::ops::Neg
    + std::cmp::Ord
    + Clone
    + Copy
    + std::fmt::Debug
    + Sized
{
    fn modulus() -> Self;
    fn zero() -> Self;
    fn one() -> Self;
}

#[derive(Clone, Copy, Debug)]
struct Modint<Value: ModValue>(Value);

impl<Value: ModValue> Modint<Value> {
    #[allow(dead_code)]
    pub fn with_value(x: Value) -> Self {
        Self(Self::normalize(x))
    }

    #[allow(dead_code)]
    pub fn with_value_unchecked(x: Value) -> Self {
        Self(x)
    }

    #[allow(dead_code)]
    pub fn validate(&self) -> &Self {
        assert!(Value::zero() <= self.0 && self.0 < Value::modulus());
        &self
    }

    fn normalize(x: Value) -> Value {
        let x = x % Value::modulus();
        if x < Value::zero() {
            x + Value::modulus()
        } else {
            x
        }
    }

    fn add_unchecked(x: Value, y: Value) -> Value {
        let z = x + y;
        if Value::modulus() <= z {
            z - Value::modulus()
        } else {
            z
        }
    }

    fn sub_unchecked(x: Value, y: Value) -> Value {
        let z = x - y;
        if x < Value::zero() {
            z + Value::modulus()
        } else {
            z
        }
    }

    fn neg_unchecked(x: Value) -> Value {
        if x == Value::zero() {
            Value::zero()
        } else {
            Value::modulus() - x
        }
    }

    fn mul_unchecked(x: Value, y: Value) -> Value {
        x * y % Value::modulus()
    }

    fn div_unchecked(x: Value, y: Value) -> Value {
        Self::mul_unchecked(x, Self::inv_unchecked(y))
    }

    fn inv_unchecked(mut x: Value) -> Value {
        let mut y: Value = Value::modulus();
        let mut u: Value = Value::one();
        let mut v: Value = Value::zero();
        while x != Value::zero() {
            let q = y / x;
            y -= x * q;
            v -= u * q;
            std::mem::swap(&mut x, &mut y);
            std::mem::swap(&mut u, &mut v);
        }
        if v < Value::zero() {
            v + Value::modulus()
        } else {
            v
        }
    }
}

macro_rules! impl_bop {
    (
        $(
            $bop_trait: ident::$bop_fn: ident, $bop_assign_trait: ident::$bop_assign_fn: ident, $impl: ident;
        )*
    ) => {
        $(
            impl<Value: ModValue> std::ops::$bop_trait for Modint<Value> {
                type Output = Self;
                fn $bop_fn(self, rhs: Self) -> Self::Output {
                    Modint(Self::$impl(self.0, rhs.0))
                }
            }
            impl<Value: ModValue> std::ops::$bop_assign_trait for Modint<Value> {
                fn $bop_assign_fn(&mut self, rhs: Self) {
                    use std::ops::$bop_trait;
                    *self = Modint::$bop_fn(*self, rhs)
                }
            }
        )*
    };
}

impl_bop! {
    Add::add, AddAssign::add_assign, add_unchecked;
    Sub::sub, SubAssign::sub_assign, sub_unchecked;
    Mul::mul, MulAssign::mul_assign, mul_unchecked;
    Div::div, DivAssign::div_assign, div_unchecked;
}

impl<Value: ModValue> std::ops::Neg for Modint<Value> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Modint(Self::neg_unchecked(self.0))
    }
}

impl<Value: ModValue> std::iter::Sum<Self> for Modint<Value> {
    fn sum<I: Iterator<Item = Self>>(iter: I) -> Self {
        iter.fold(Self::with_value(Value::zero()), std::ops::Add::add)
    }
}

impl<Value: ModValue + Default> std::default::Default for Modint<Value> {
    fn default() -> Self {
        Self::with_value(Default::default())
    }
}

impl<Value: ModValue> std::convert::From<Value> for Modint<Value> {
    fn from(x: Value) -> Self {
        Self::with_value(x)
    }
}

#[allow(unused_macros)]
macro_rules! modint {
    ($x: expr) => {
        Modint::with_value($x)
    };
}

#[allow(unused_macros)]
macro_rules! impl_modvalue {
    ($(const $mod_name: ident: $ty: ty = $mod_value: expr;)*) => {
        $(
            const $mod_name: $ty = $mod_value;
            impl ModValue for $ty {
                fn modulus() -> Self {
                    $mod_name
                }
                fn zero() -> Self {
                    0
                }
                fn one() -> Self {
                    1
                }
            }
        )*
    };
}

impl_modvalue! {
    const MOD: i64 = 1_000_000_007;
}

mod segtree {
    #[allow(dead_code)]
    pub(super) trait SegTrait {
        type Value: Clone;
        fn id() -> Self::Value;
        fn op(x: &Self::Value, y: &Self::Value) -> Self::Value;
    }

    #[allow(dead_code)]
    pub(super) struct Segtree<T: SegTrait> {
        table: Vec<T::Value>,
    }

    #[allow(dead_code)]
    impl<T: SegTrait> Segtree<T> {
        pub fn new(n: usize) -> Self {
            Self {
                table: vec![T::id(); 2 * n],
            }
        }

        pub fn size(&self) -> usize {
            self.table.len() / 2
        }

        pub fn map(&mut self, mut i: usize, f: impl Fn(&mut T::Value)) {
            let n = self.size();
            assert!(i < n);
            i += n;
            f(&mut self.table[i]);
            i /= 2;
            while 0 < i {
                let tmp = T::op(&self.table[2 * i], &self.table[2 * i + 1]);
                self.table[i] = tmp;
                i /= 2;
            }
        }

        pub fn add(&mut self, i: usize, x: T::Value)
        where
            T::Value: Copy + std::ops::AddAssign,
        {
            self.map(i, |y| *y += x);
        }

        pub fn fold(&self, mut l: usize, mut r: usize) -> T::Value {
            let n = self.size();
            assert!(l <= r && r <= n);
            l += n;
            r += n;
            let mut ans = T::id();
            while l < r {
                if l % 2 == 1 {
                    ans = T::op(&ans, &self.table[l]);
                    l += 1;
                }
                if r % 2 == 1 {
                    r -= 1;
                    ans = T::op(&ans, &self.table[r]);
                }
                l /= 2;
                r /= 2;
            }
            ans
        }

        pub fn fold_all(&self) -> T::Value {
            let n = self.size();
            self.fold(0, n)
        }

        pub fn collect(&self) -> Vec<T::Value> {
            let n = self.size();
            self.table[n..].into()
        }
    }

    pub(super) struct AddMonoid<T: std::ops::Add<Output = T>> {
        phantom: std::marker::PhantomData<T>,
    }
    impl<T: Clone + std::ops::Add<Output = T> + std::default::Default> SegTrait for AddMonoid<T> {
        type Value = T;

        fn id() -> Self::Value {
            T::default()
        }

        fn op(x: &T, y: &T) -> T {
            x.clone() + y.clone()
        }
    }
}

fn main() {
    input!(n: usize, m: usize, a: [i64; n], b: [i64; m]);
    let mut data = vec![];
    let mut j = 0;
    for i in 0..n {
        while j < m && b[j] < a[i] {
            j += 1;
        }
        if j == 0 {
            continue;
        }
        if j == m {
            break;
        }
        data.push((a[i] - b[j - 1], b[j] - a[i]));
    }

    let mut coord_x: Vec<_> = data.iter().map(|&(x, _)| x).collect();
    let mut coord_y: Vec<_> = data.iter().map(|&(_, y)| y).collect();
    coord_x.sort();
    coord_y.sort();
    coord_x.dedup();
    coord_y.dedup();

    data.sort_by_key(|&(x, y)| (x, std::cmp::Reverse(y)));
    data.dedup();
    let data: Vec<_> = data
        .iter()
        .map(|&(x, y)| {
            (
                coord_x.binary_search(&x).unwrap(),
                coord_y.binary_search(&y).unwrap(),
            )
        })
        .collect();

    let mut seg = segtree::Segtree::<segtree::AddMonoid<Modint<i64>>>::new(n + 1);
    seg.add(0, modint!(1));
    for &(_, i) in &data {
        let crr = seg.fold(0, i + 1);
        seg.add(i + 1, crr);
    }
    println!("{}", seg.fold_all().0);
}