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: Runtime Error)

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
class Solve{
    public Solve(){}
    StringBuilder sb;
    public static int Main(){
        new Solve().Run();
        return 0;
    }
    void Run(){
        sb = new StringBuilder();
        Calc();
        Console.Write(sb.ToString());
    }
    void Calc(){
        string[] str = Console.ReadLine().Split(' ');
        int N = int.Parse(str[2]);
        long RR = int.Parse(str[0]);
        long C = int.Parse(str[1]);
        int[] A;
        {
            int count = 0;
            List<long> Left = new List<long>();
            List<long> Right = new List<long>();
            List<long> Top = new List<long>();
            List<long> Bottom = new List<long>();
            List<int> left = new List<int>();
            List<int> right = new List<int>();
            List<int> top = new List<int>();
            List<int> bottom = new List<int>();
            for(int i=0;i<N;i++){
                str = Console.ReadLine().Split(' ');
                long X1 = int.Parse(str[0]);
                long Y1 = int.Parse(str[1]);
                long X2 = int.Parse(str[2]);
                long Y2 = int.Parse(str[3]);
                bool bb = X1 == 0 || X1 == RR || Y1 == 0 || Y1 == C;
                bool c = X2 == 0 || X2 == RR || Y2 == 0 || Y2 == C;
                if(bb && c){
                    count++;
                    if(X1 == 0){
                        Left.Add(Y1);
                        left.Add(i);
                    }
                    else if(X1 == RR){
                        Right.Add(Y1);
                        right.Add(i);
                    }
                    else if(Y1 == 0){
                        Bottom.Add(X1);
                        bottom.Add(i);
                    }
                    else{
                        Top.Add(X1);
                        top.Add(i);
                    }
                    if(X2 == 0){
                        Left.Add(Y2);
                        left.Add(i);
                    }
                    else if(X2 == RR){
                        Right.Add(Y2);
                        right.Add(i);
                    }
                    else if(Y2 == 0){
                        Bottom.Add(X2);
                        bottom.Add(i);
                    }
                    else{
                        Top.Add(X2);
                        top.Add(i);
                    }
                }
            }
            long[] L = Left.ToArray();
            long[] R = Right.ToArray();
            long[] T = Top.ToArray();
            long[] B = Bottom.ToArray();
            int[] l = left.ToArray();
            int[] r = right.ToArray();
            int[] t = top.ToArray();
            int[] b = bottom.ToArray();
            Array.Sort(L,l);
            Array.Sort(R,r);
            Array.Sort(T,t);
            Array.Sort(B,b);
            A = new int[2*count];
            count = 0;
            for(int i=0;i<l.Length;i++){
                A[count] = l[i];
                count++;
            }
            for(int i=0;i<t.Length;i++){
                A[count] = t[i];
                count++;
            }
            for(int i=0;i<r.Length;i++){
                A[count] = r[r.Length-1-i];
                count++;
            }
            for(int i=0;i<b.Length;i++){
                A[count] = b[b.Length-1-i];
                count++;
            }
        }
        if(A.Length == 0){
            sb.Append("YES\n");
            return;
        }
        int p = -1;
        int q = -1;
        if(A[0] == A[A.Length-1]){
            p = 0;
            q = A.Length-1;
        }
        else{
            for(int i=0;i<A.Length-1;i++){
                if(A[i] == A[i+1]){
                    p = i+1;
                    q = i;
                }
            }
        }
        if(p == -1){
            sb.Append("NO\n");
            return;
        }
        while(true){
            p++;
            q--;
            if(q == -1){
                q = A.Length-1;
            }
            if(p == A.Length){
                p = 0;
            }
            if((p == A.Length-1 && q == 0) || p+1 == q){
                if(A[p] == A[q]){
                    sb.Append("YES\n");
                }
                else{
                    sb.Append("NO\n");
                }
                return;
            }
            if(A[p] != A[q]){
                sb.Append("NO\n");
                return;
            }
        }
    }
}