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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.IO;

namespace SolutionCS
{
    class Program
    {
        class modMCalc
        {
            private const int M = 1000000007;       // 素数(int範囲)

            // 乗算結果 % Mを求める
            static public int Mul(int a, int b)
            {
                return (int)(Math.BigMul(a, b) % M);
            }

            // 冪乗(mod M)
            static public int Pow(int a, int m)
            {
                if (0 == m)
                {
                    return 1;
                }
                if (1 == m)
                {
                    return a;
                }

                int result = 1;
                int param = a;
                for (; m > 0; m >>= 1)
                {
                    if ((m & 0x01) == 1)
                    {
                        result = Mul(result, param);
                    }

                    param = Mul(param, param);
                }

                return result;
            }

            // 除算(mod M)
            static public int Div(int a, int b)
            {
                return Mul(a, Pow(b, M - 2));
            }
        }

        class CombinationPermutation
        {
            private readonly int[] fac;

            // コンストラクタ
            // (mod Mの)階乗テーブル作成する
            public CombinationPermutation(int n)
            {
                fac = new int[n + 1];
                fac[0] = 1;
                for (int i = 1; i <= n; ++i)
                {
                    fac[i] = modMCalc.Mul(fac[i - 1], i);
                }
            }

            // 階乗求める
            public int Factorial(int n)
            {
                return fac[n];
            }

            // nPr
            public int nPr(int n, int r)
            {
                if (n < r)
                {
                    return 0;
                }

                if (n == r)
                {
                    return 1;
                }

                int result = Factorial(n);
                result = modMCalc.Div(result, Factorial(n - r));

                return result;
            }

            // nCr
            public int nCr(int n, int r)
            {
                return modMCalc.Div(nPr(n, r), Factorial(r));
            }
        }

        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int N = int.Parse(s[0]);
            int M = int.Parse(s[1]);

            if ( Math.Abs(N-M) >= 2)
            {
                Console.WriteLine(0);
                return;
            }

            CombinationPermutation perm = new CombinationPermutation(Math.Max(N, M));

            int ans = perm.Factorial(N);
            ans = modMCalc.Mul(ans, perm.Factorial(M));

            if (N == M)
            {
                ans = modMCalc.Mul(ans, 2);
            }

            Console.WriteLine(ans);
        }
    }
}