Problem:
Score : 900 points

Problem Statement

Joisino has a lot of red and blue bricks and a large box.
She will build a tower of these bricks in the following manner.

First, she will pick a total of N bricks and put them into the box.
Here, there may be any number of bricks of each color in the box, as long as there are N bricks in total.
Particularly, there may be zero red bricks or zero blue bricks.
Then, she will repeat an operation M times, which consists of the following three steps:

Take out an arbitrary brick from the box.

Put one red brick and one blue brick into the box.

Take out another arbitrary brick from the box.

After the M operations, Joisino will build a tower by stacking the 2 \times M bricks removed from the box, in the order they are taken out.
She is interested in the following question: how many different sequences of colors of these 2 \times M bricks are possible?
Write a program to find the answer.
Since it can be extremely large, print the count modulo 10^9+7.

Constraints

1 \leq N \leq 3000

1 \leq M \leq 3000

Input

Input is given from Standard Input in the following format:

N M

Output

Print the count of the different possible sequences of colors of 2 \times M bricks that will be stacked, modulo 10^9+7.

Sample Input 1

2 3

Sample Output 1

56

A total of six bricks will be removed from the box. The only impossible sequences of colors of these bricks are the ones where the colors of the second, third, fourth and fifth bricks are all the same. Therefore, there are 2^6 - 2 \times 2 \times 2 = 56 possible sequences of colors.

Sample Input 2

1000 10

Sample Output 2

1048576

Sample Input 3

1000 3000

Sample Output 3

693347555

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

// use std::ops::{Index, IndexMut};
// use std::cmp::{Ordering, min, max};
// use std::collections::{BinaryHeap, BTreeMap};
// use std::collections::btree_map::Entry::{Occupied, Vacant};
// use std::clone::Clone;

fn getline() -> String{
    let mut res = String::new();
    std::io::stdin().read_line(&mut res).ok();
    res
}

macro_rules! readl {
    ($t: ty) => {
        {
            let s = getline();
            s.trim().parse::<$t>().unwrap()
        }
    };
    ($( $t: ty),+ ) => {
        {
            let s = getline();
            let mut iter = s.trim().split(' ');
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*) 
        }
    };
}

macro_rules! readlvec {
    ($t: ty) => {
        {
            let s = getline();
            let iter = s.trim().split(' ');
            iter.map(|x| x.parse().unwrap()).collect::<Vec<$t>>()
        }
    }
}

macro_rules! mvec {
    ($v: expr, $s: expr) => {
        vec![$v; $s]
    };
    ($v: expr, $s: expr, $($t: expr),*) => {
        vec![mvec!($v, $($t),*); $s]
    };
}

macro_rules! debug {
    ($x: expr) => {
        println!("{}: {:?}", stringify!($x), $x)
    }
}

fn printiter<'a, T>(v: &'a T)
where
    &'a T: std::iter::IntoIterator, 
    <&'a T as std::iter::IntoIterator>::Item: std::fmt::Display {
    for (i,e) in v.into_iter().enumerate() {
        if i != 0 {
            print!(" ");
        }
        print!("{}", e);
    }
    println!("");
}

struct UnionFind {
    par: Vec<usize>,
    rank: Vec<usize>,
    cnt: Vec<u32>,
}

impl UnionFind {
    fn new(n: usize) -> UnionFind {
        let mut vec = vec![0;n];
        for i in 0..n {
            vec[i] = i;
        }
        UnionFind {
            par : vec,
            rank : vec![0;n],
            cnt : vec![0;n],
        }
    }

    fn find(&mut self, x: usize) -> usize {
        if x == self.par[x] {
            x
        }else{
            let par = self.par[x];
            let res = self.find(par);
            self.par[x] = res;
            res
        }
    }

    fn same(&mut self, a: usize, b: usize) -> bool {
        self.find(a) == self.find(b)
    }

    fn unite(&mut self, a: usize, b: usize){
        let apar = self.find(a);
        let bpar = self.find(b);
        if apar == bpar {
            return;
        }
        if self.rank[apar] > self.rank[bpar] {
            self.par[bpar] = apar;
            self.cnt[apar] += self.cnt[bpar];
        }else{
            self.par[apar] = bpar;
            self.cnt[bpar] += self.cnt[apar];
            if self.rank[apar] == self.rank[bpar] {
                self.rank[bpar] += 1;
            }
        }
    }

    fn count(&mut self, v: usize) -> u32 {
        let par = self.find(v);
        self.cnt[par]
    }

    fn add(&mut self, v: usize, x: u32) {
        let par = self.find(v);
        self.cnt[par] += 1;
    }
}

fn main() {
    let n = readl!(usize);
    let a = readlvec!(i32);
    let b = readlvec!(i32);
    let axor = a.iter().fold(0, |sum, &x| sum^x);
    let mut acnt = std::collections::HashMap::new();
    for &e in &a {
        let x = acnt.entry(e).or_insert(0);
        *x += 1;
    }
    {
        let x = acnt.entry(axor).or_insert(0);
        *x += 1;
    }
    let mut ex = true;
    let mut usea = false;
    // let mut cnt = 0;
    let mut deg = vec![0; n];
    // debug!(acnt);
    for i in 0..n {
        {
            // cnt += \1;
            if axor == b[i] && !usea {
                usea = true;
            }
            let x = acnt.entry(b[i]).or_insert(0);
            if *x == 0 {
                ex = false;
                break;
            }
            *x -= 1;
        }
    }
    debug!(acnt);
    if !ex {
        println!("-1");
        return;
    }
    
    use std::collections::HashSet;
    let mut za = a.clone();
    za.push(axor);
    za.sort();
    za.dedup();
    // debug!(za);
    let mut uf = UnionFind::new(za.len());
    for (&a, &b) in a.iter().zip(b.iter()) {
        if a == b {
            continue;
        }
        let ap = za.binary_search(&a).unwrap();
        let bp = za.binary_search(&b).unwrap();
        // debug!(ap); debug!(bp);
        uf.unite(ap, bp);
        uf.add(ap, 1);
    }
    uf.add(za.binary_search(&axor).unwrap(), 1);
    // debug!(uf.cnt);

    let mut used = vec![false; za.len()];
    let mut ans = 0;
    for i in 0..za.len() {
        let par = uf.find(i);
        // debug!(par);
        if !used[par] {
            used[par] = true;
            let num = uf.count(i);
            // debug!(num);
            if num > 1 {
                ans += num+1;
            }
        }
        // debug!(ans);
    }
    if usea {
        println!("{}", ans-2);
    } else {
        println!("{}", ans);
    }
}