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)

use std::io::{BufRead, Write};
use std::io;
use std::ops::{Add, Sub, Mul, Div};

const MOD: u64 = 1000000007;

#[derive(Clone, Copy, Debug)]
struct ModInt(u64);

impl ModInt {
    fn pow(self, exp: u64) -> ModInt {
        match exp {
            0 => ModInt(1),
            1 => self,
            x if x % 2 == 0 => ModInt(self.pow(x / 2).0.pow(2)),
            x => ModInt(self.pow(x / 2).0.pow(2)) * self,
        }
    }

    fn permutation(mut lhs: u64, rhs: u64) -> ModInt {
        let mut result = ModInt(1);
        for _ in 0..rhs {
            result = result * ModInt(lhs);
            lhs -= 1;
        }
        result
    }
}

impl From<u64> for ModInt {
    fn from(x: u64) -> Self {
        ModInt(x % MOD)
    }
}

impl Into<u64> for ModInt {
    fn into(self) -> u64 {
        self.0
    }
}

impl Add for ModInt {
    type Output = ModInt;

    fn add(self, rhs: Self) -> ModInt {
        let sum = self.0 + rhs.0;
        ModInt(if sum > MOD { sum - MOD } else { sum })
    }
}

impl Sub for ModInt {
    type Output = ModInt;

    fn sub(self, rhs: Self) -> ModInt {
        ModInt(if self.0 < rhs.0 {
            self.0 + MOD - rhs.0
        } else {
            self.0 - rhs.0
        })
    }
}

impl Mul for ModInt {
    type Output = ModInt;

    fn mul(self, rhs: Self) -> ModInt {
        ModInt(self.0 * rhs.0 % MOD)
    }
}

impl Div for ModInt {
    type Output = ModInt;

    fn div(self, rhs: Self) -> ModInt {
        self * rhs.pow(MOD - 2)
    }
}

fn solve<R: BufRead, W: Write>(mut input: R, mut output: W) {
    let mut s = String::new();
    input.read_line(&mut s).unwrap();
    let mut split = s.split_whitespace();
    let n: u64 = split.next().unwrap().parse().unwrap();
    let m: u64 = split.next().unwrap().parse().unwrap();
    let result: u64 = if n == m {
        ModInt::permutation(n, n) * ModInt::permutation(m, m) * 2.into()
    } else if n == m + 1 || n + 1 == m {
        ModInt::permutation(n, n) * ModInt::permutation(m, m)
    } else {
        0.into()
    }.into();
    writeln!(output, "{}", result).unwrap();
}

fn main() {
    let stdin = io::stdin();
    let stdout = io::stdout();
    solve(stdin.lock(), stdout.lock());
}

#[cfg(test)]
mod tests {
    fn test(input: &str, output: &str) {
        let mut v: Vec<u8> = Vec::new();
        ::solve(input.as_bytes(), &mut v);
        assert_eq!(String::from_utf8(v).unwrap(), output);
    }
    #[test]
    fn case1() {
        test("2 2\n", "8\n");
    }
    #[test]
    fn case2() {
        test("3 2\n", "12\n");
    }
    #[test]
    fn case3() {
        test("1 8\n", "0\n");
    }
    #[test]
    fn case4() {
        test("100000 100000\n", "530123477\n");
    }
}