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;

#define endl '\n'
#define ff first
#define ss second
#define mp make_pair
#define pb push_back

typedef long long llong;
typedef pair<int, int> pii;

int find(vector<int> &parent, int cur) {
	if (cur == parent[cur]) return cur;
	else return find(parent, parent[cur]);
}

void solve() {

	srand(time(0));

	int n;
	cin >> n;

	// coordinate, vertex
	vector<pii> x_coord(n), y_coord(n);
	// x, y coordinate combo
	vector<pii> vertexes(n);

	for (int i = 0; i < n; i++) {
		int x, y;
		cin >> x >> y;
		x_coord[i] = mp(x, i);
		y_coord[i] = mp(y, i);
		vertexes[i] = mp(x, y);
	}

	sort(x_coord.begin(), x_coord.end());
	sort(y_coord.begin(), y_coord.end());

	vector< pair<int, pii> > edges;

	// x coordinates
	for (int i = 1; i < n; i++) {
		int u = x_coord[i - 1].ss;
		int v = x_coord[i].ss;
		int weight = x_coord[i].ff - x_coord[i - 1].ff;
		edges.pb(mp(weight, mp(u, v)));
	}

	// y coordinates
	for (int i = 1; i < n; i++) {
		int u = y_coord[i - 1].ss;
		int v = y_coord[i].ss;
		int weight = y_coord[i].ff - y_coord[i - 1].ff;
		edges.pb(mp(weight, mp(u, v)));
	}
	sort(edges.begin(), edges.end());


	// disjoint union set
	vector<int> parent(n), rank(n);
	for (int i = 0; i < n; i++) parent[i] = i;

	int ans = 0;

	for (const auto &edge : edges) {

		int weight = edge.ff;
		int u = edge.ss.ff;
		int v = edge.ss.ss;

		// cout << u << ' ' << v << " : " << weight << endl;

		int up = find(parent, u);
		int vp = find(parent, v);

		// disjoint
		if (up != vp) {

			bool lul = rand() % 2;

			if (lul) {
				parent[vp] = up;
			} else {
				parent[up] = vp;
			}

			// if (rank[up] > rank[vp]) {
			// 	parent[vp] = up;
			// } else if (rank[vp] > rank[up]) {
			// 	parent[up] = vp;
			// } else {
			// 	parent[up] = vp;
			// 	rank[vp]++;
			// }
			// parent[vp] = up;
			ans += weight;
		}
	}

	cout << ans;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	#ifdef LOCAL
		ifstream in("in");
		cin.rdbuf(in.rdbuf());
	#endif

	solve();
}