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 (Python):
def init_fact(n):
    ret=[0]*(n+1)
    ret[0]=1
    for i in xrange(1,n+1):
        ret[i]=ret[i-1]*i%mod
    return ret

mod=10**9+7
n,m=map(int,raw_input().split())
if abs(n-m)>=2:
    print 0
    exit()
fact=init_fact(10**5+5)
if n==m:
    print fact[n]*fact[m]*2%mod
else:
    print fact[n]*fact[m]%mod

Solution (Python):
import math
N,M=map(int,input().split())
if abs(N-M)>1:
  print(0)
elif abs(N-M)==1:
  n=math.factorial(N)%(10**9+7)
  m=math.factorial(M)%(10**9+7)
  print((n*m)%(10**9+7))
else:
  n=math.factorial(N)%(10**9+7)
  m=math.factorial(M)%(10**9+7)
  print((2*n*m)%(10**9+7))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N,M = sorted(LI())
    if abs(N-M) > 1:
        return 0
    nr = 1
    for i in range(2,N+1):
        nr *= i
        nr %= mod
    if N == M:
        nr = nr * nr
        nr %= mod
        nr *= 2
        nr %= mod
        r = nr
    else:
        mr = nr * M
        mr %= mod
        r = nr * mr
        r %= mod

    return r

print(main())

Solution (Python):
n, m = map(int, input().split())
if m<n-1 or n+1<m:
    print(0)
    exit()
MOD = 10**9+7

ans = 1
for i in range(2, n+1):
    ans = (ans * i)%MOD
for i in range(2, m+1):
    ans = (ans * i)%MOD
if n == m:
    ans = (ans * 2)%MOD
print(ans)

Solution (Python):
n, m = map(int, input().split())

MOD = 10**9 + 7
if 1 < abs(n - m):
    ans = 0
else:
    ans = 1
    for i in range(1, n + 1):
        ans *= i
        ans %= MOD
    for i in range(1, m + 1):
        ans *= i
        ans %= MOD
    if n == m:
        ans *= 2
        ans %= MOD
print(ans)

Solution (Python):
n, m = map(int, raw_input().split())
MOD = 10**9+7

F = [0 for _ in xrange(10**5+10)]
F[0] = 1
F[1] = 1
for i in xrange(2, 10**5+2):
    F[i] = F[i-1] * i % MOD

if abs(n - m) >= 2:
    print 0
elif abs(n - m) == 1:
    print F[n] * F[m] % MOD
else:
    print F[n] * F[m] * 2 % MOD

Solution (Python):

import sys

input=sys.stdin.readline

n,m=map(int,input().split())

if n==m: cnt=2
elif abs(n-m)==1: cnt=1
else: cnt=0


for i in range(1,n+1): cnt=(cnt*i)%(1000000007)
for i in range(1,m+1): cnt=(cnt*i)%(1000000007)

print(cnt)