Problem:
Score : 300 points

Problem Statement

Snuke has N dogs and M monkeys. He wants them to line up in a row.

As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.

How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.

Constraints

1 ≤ N,M ≤ 10^5

Input

Input is given from Standard Input in the following format:

N M

Output

Print the number of possible arrangements, modulo 10^9+7.

Sample Input 1

2 2

Sample Output 1

8

We will denote the dogs by A and B, and the monkeys by C and D. There are eight possible arrangements: ACBD, ADBC, BCAD, BDAC, CADB, CBDA, DACB and DBCA.

Sample Input 2

3 2

Sample Output 2

12

Sample Input 3

1 8

Sample Output 3

0

Sample Input 4

100000 100000

Sample Output 4

530123477

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

mod modint {

use std;
use std::fmt;
use std::num::ParseIntError;
use std::ops;
use std::str::FromStr;

#[derive(PartialEq, Eq, Debug, Clone)]
pub struct Modint {
    p: usize, //
    v: usize,
}

const DEFAULT_MODNUM: usize = 1_000_000_007;

impl Modint {
    #[allow(dead_code)]
    pub fn new(p: usize) -> Modint {
        Modint { p: p, v: 0 }
    }
    #[allow(dead_code)]
    pub fn from(p: usize, v: usize) -> Modint {
        Modint { p: p, v: v % p }
    }
}

impl FromStr for Modint {
    type Err = ParseIntError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let num: usize;
        num = s.parse()?;
        Ok(Modint {
            p: DEFAULT_MODNUM,
            v: num,
        })
    }
}

//print用
impl fmt::Display for Modint {
    fn fmt<'a>(&self, f: &mut fmt::Formatter<'a>) -> fmt::Result {
        write!(f, "{}", self.v)
    }
}

//演算子関係
impl<'a> ops::Add for &'a Modint {
    type Output = Modint;

    #[allow(dead_code)]
    fn add(self, other: &'a Modint) -> Modint {
        if self.p != other.p {
            panic!("Mod Error");
        }
        Modint {
            p: self.p,
            v: (self.v + other.v) % self.p,
        }
    }
}

impl<'a> ops::Sub for &'a Modint {
    type Output = Modint;

    #[allow(dead_code)]
    fn sub(self, other: &'a Modint) -> Modint {
        if self.p != other.p {
            panic!("Mod Error");
        }
        Modint {
            p: self.p,
            v: (self.v + self.p - (other.v % self.p)) % self.p,
        }
    }
}

impl<'a> ops::Mul for &'a Modint {
    type Output = Modint;

    #[allow(dead_code)]
    fn mul(self, other: &'a Modint) -> Modint {
        if self.p != other.p {
            panic!("Mod Error");
        }
        Modint {
            p: self.p,
            v: (self.v * other.v) % self.p,
        }
    }
}

impl<'a> ops::Div for &'a Modint {
    type Output = Modint;

    fn div(self, other: &'a Modint) -> Modint {
        if self.p != other.p {
            panic!("Mod Error");
        }
        let mut obj = self.clone();
        obj.div_uint(other.v);
        obj
    }
}

//ToDo
//AddAssign,SubAssign,MulAssign,DivAssignを実装

impl Modint {
    #[allow(dead_code)]
    pub fn add_uint(&mut self, n: usize) -> &Self {
        self.v += n;
        self.v %= self.p;
        self
    }

    #[allow(dead_code)]
    pub fn sub_uint(&mut self, mut n: usize) -> &Self {
        n = n % self.p;
        self.v += self.p;
        self.v -= n;
        self.v %= self.p;
        self
    }

    #[allow(dead_code)]
    pub fn mul_uint(&mut self, n: usize) -> &Self {
        self.v *= n;
        self.v %= self.p;
        self
    }

    #[allow(dead_code)]
    pub fn div_uint(&mut self, n: usize) -> &Self {
        let mut obj = Modint::from(self.p, n);
        obj.inv();
        self.v *= obj.v;
        self.v %= self.p;
        self
    }

    #[allow(dead_code)]
    pub fn inv(&mut self) -> &Self {
        let p = self.p;
        self.pow(p - 2);
        self
    }

    #[allow(dead_code)]
    pub fn pow(&mut self, mut n: usize) -> &Self {
        let mut temp = self.v;
        self.v = 1;
        while n > 0 {
            if n & 1 != 0 {
                self.v *= temp;
                self.v %= self.p;
            }
            n >>= 1;
            temp *= temp;
            temp %= self.p;
        }
        self
    }
}

pub struct DPFactorial {
    p: usize,
    normal: Vec<Modint>,
    inv: Vec<Modint>,
}
impl DPFactorial {
    #[allow(dead_code)]
    pub fn new(p: usize) -> DPFactorial {
        let mut obj = DPFactorial {
            p: p,
            normal: Vec::new(),
            inv: Vec::new(),
        };
        obj.normal.push(Modint::from(p, 1));
        obj.inv.push(Modint::from(p, 1));
        obj
    }

    #[allow(dead_code)]
    pub fn get_factorial(&mut self, n: usize) -> Modint {
        if n < self.normal.len() {
            return self.normal[n].clone();
        }
        for z in self.normal.len()..n + 1 {
            let buf = Modint::from(self.p, z);
            let buf = &buf * &self.normal[z - 1];

            self.normal.push(buf);
        }
        return self.normal[n].clone();
    }

    #[allow(dead_code)]
    pub fn get_factorial_inv(&mut self, n: usize) -> Modint {
        if n < self.inv.len() {
            return self.inv[n].clone();
        }
        for z in self.inv.len()..n + 1 {
            let mut buf = Modint::from(self.p, z);
            buf.inv();
            let buf = &buf * &self.inv[z - 1];
            self.inv.push(buf);
        }
        self.inv[n].clone()
    }

    #[allow(dead_code)]
    pub fn get_combination(&mut self, n: usize, r: usize) -> Modint {
        if n < r {
            return Modint::from(self.p, 0);
        }
        &(&self.get_factorial(n) * &(self.get_factorial_inv(n - r))) * &(self.get_factorial_inv(r))
    }

    #[allow(dead_code)]
    pub fn get_permutation(&mut self, n: usize, r: usize) -> Modint {
        if n < r {
            return Modint::from(self.p, 0);
        }
        &self.get_factorial(n) * &(self.get_factorial_inv(n - r))
    }
}
}



//提出時は下二行と一番下のコメントアウトを外す。
#[macro_use]
mod input {

use std;
use std::io;

const SPLIT_DELIMITER: char = ' ';

/// 空白で区切られた複数の値の読み込む。
/// # Example
/// ```ignore
/// use cp_template::*;
///
/// let (a,b):(usize,usize);
/// input!(a,b);
/// ```
#[macro_export]
#[allow(unused_macros)]
macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

/// 文字列を一行読み込む
/// # Example
/// ```ignore
/// use cp_template::*;
///
/// let s = input_line_str();
/// ```
#[allow(dead_code)]
pub fn input_line_str() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

/// 一つの変数を出力する。
/// ```
/// use cp_template::*;
///
/// let a=100;
/// p(a);
/// //上と下は等価です。
/// println!("{}",a);
/// ```
#[allow(dead_code)]
pub fn p<T>(t: T)
where
    T: std::fmt::Display,
{
    println!("{}", t);
}

/// 指定した行数を読み込み、二次元配列に変換する。
/// # Examples
/// ```ignore
/// use cp_template::*;
///
/// //10行読み込む。
/// let v = input_vector2d::<usize>(10);
/// ```
#[allow(dead_code)]
pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
where
    T: std::str::FromStr,
{
    let mut v: Vec<Vec<T>> = Vec::new();

    for _ in 0..line {
        let vec_line = input_vector();
        v.push(vec_line);
    }
    v
}

/// 一行読み込み、配列(Vec)に変換する。
/// # Examples
/// ```ignore
/// use cp_template::*;
///
/// let v=input_vector::<usize>();
/// ```
#[allow(dead_code)]
pub fn input_vector<T>() -> Vec<T>
where
    T: std::str::FromStr,
{
    let mut v: Vec<T> = Vec::new();

    let s = input_line_str();
    let split_result = s.split(SPLIT_DELIMITER);
    for z in split_result {
        let buf = match z.parse() {
            Ok(r) => r,
            Err(_) => panic!("Parse Error"),
        };
        v.push(buf);
    }
    v
}

/// String(str)を配列(Vec<char>)に変換する。
/// # Examples
/// ```ignore
/// use cp_template::*;
///
/// let s = String::from("abc");
/// let v = str2vec(&s);
/// let v = str2vec(&input_line_str());
/// ```
#[allow(dead_code)]
pub fn str2vec(s: &str) -> Vec<char> {
    let mut v: Vec<char> = Vec::new();
    for c in s.chars() {
        v.push(c);
    }
    v
}

}

use std::cmp;
use std::mem::swap;

//提出用
#[allow(unused_imports)]
use input::*;
#[allow(unused_imports)]
use modint::*;

fn main() {
    let (mut m, mut n): (usize, usize);
    input!(m, n);
    let mut f = DPFactorial::new(1_000_000_007);
    if m > n {
        swap(&mut m, &mut n);
    }
    if n - m > 1 {
        println!("0");
        return;
    }
    if n == m {
        println!(
            "{}",
            (&f.get_factorial(m) * &f.get_factorial(n)).mul_uint(2)
        );
    } else {
        println!("{}", (&f.get_factorial(m) * &f.get_factorial(n)));
    }
}