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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ull = unsigned long long;

#include <bits/stdc++.h>

namespace NyaGadgetModLL
{	// MOD ライブラリ
	using namespace std;

	template<long long mod> struct ModLL
	{
		long long x;

		// コンストラクタ
		ModLL()
		{
			x = 0;
		}
		ModLL(long long x_)
		{
			x = x_ % mod + mod;
			if (x >= mod)
				x -= mod;
		}

		// 符号
		ModLL operator + () const { return x; }
		ModLL operator - () const { return (-x < 0) ? mod - x : -x; }

		// 加減乗除演算子
		ModLL& operator += (ModLL r)
		{
			if ((x += r.x) >= mod)
				x -= mod;
			return *this;
		}
		ModLL & operator -= (ModLL r)
		{
			if ((x -= r.x) < 0)
				x += mod;
			return *this;
		}
		ModLL& operator *= (ModLL r)
		{
			x = (unsigned long long) x * r.x % mod;
			return *this;
		}
		ModLL& operator /= (ModLL r)
		{
			x = x * Inv(r.x, mod) % mod;
			return *this;
		}

		ModLL operator + (ModLL r) const { return ModLL(*this) += r; }
		ModLL operator - (ModLL r) const { return ModLL(*this) -= r; }
		ModLL operator * (ModLL r) const { return ModLL(*this) *= r; }
		ModLL operator / (ModLL r) const { return ModLL(*this) /= r; }

		// 逆元 x^{-1} (主に除算演算子で使用)
		long long Inv(long long a, long long m)
		{
			long long b = m, u = 1, v = 0;

			while (b)
			{
				long long t = a / b;
				a -= t * b; swap(a, b);
				u -= t * v; swap(u, v);
			}

			u %= m;

			return (u < 0) ? u + m : u;
		}

		// 比較演算子
		bool operator == (ModLL & r) const { return x == r.x; }
		bool operator != (ModLL & r) const { return x != r.x; }
		bool operator <  (ModLL & r) const { return x < r.x; }
		bool operator <= (ModLL & r) const { return x <= r.x; }
		bool operator >  (ModLL & r) const { return x > r.x; }
		bool operator >= (ModLL & r) const { return x >= r.x; }

		// 入出力演算子
		friend ostream& operator << (ostream & s, ModLL<mod> a)
		{
			s << a.x;
			return s;
		}
		friend istream& operator >> (istream & s, ModLL<mod> & a)
		{
			s >> a.x;
			return s;
		}
	};

	//*******************************************
	// [sample code]
	//
	//using namespace NyaGadgetModLL;
	//using mll = ModLL<1000000007>;
	//
	//int main(void)
	//{
	//	mll x, y(1000000009);
	//  mll div_x(12345678900000);
	//  mll div_y(100000);
	//
	//	div_x /= div_y;
	//
	//	cout << "x = " << x << endl;
	//  cout << "y = " << y << endl;
	//  cout << "div_x = " << div_x << endl;
	//	return 0;
	//}
	//*******************************************
	// [output]
	//
	// x = 0
	// y = 2
	// div_x = 123456789
	//*******************************************
}


#ifndef __MACRO_H__
#define __MACRO_H__

#define all(collection) (collection).begin(), (collection).end()  // begin to end
#define rep(i, begin, end) for (ll i = begin; i < end; i++)       // repeat
#define repr(i, begin, end) for (ll i = begin; end < i; i--)      // repeat reverse
#define size(collection) ((ll) (collection).size())               // collection size

#endif

using namespace NyaGadgetModLL;
using mll = ModLL<1000000007>;

int main(void)
{
	mll n, m; cin >> n >> m;

	mll sub = 1;
	if (n < m && m - n != sub)
	{
		cout << 0;
		return 0;
	}
	else if (m < n && n - m != sub)
	{
		cout << 0;
		return 0;
	}
	
	mll ans = 1;
	mll max = 1;
	if (m != n)
	{
		ans = m;
		max = m;

		cout << ans << endl;
		cout << max << endl;
		for (mll i = 1; i < max; i += 1)
		{
			m -= 1;
			ans *= m;
		}
		ans *= n;
		for (mll i = 1; i < max; i += 1)
		{
			n -= 1;
			ans *= n;
		}
	}
	else
	{
		ans = m;
		max = m;
		for (mll i = 1; i < max; i += 1)
		{
			m -= 1;
			ans *= m;
		}
		ans *= n;
		for (mll i = 1; i < max; i += 1)
		{
			n -= 1;
			ans *= n;
		}
		ans *= 2;
	}

	cout << ans;
	return 0;
}