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):
import math
N,M = map(int,input().split())
if abs(N - M) > 1:
  print(0)
elif N == M:
  print((2*(math.factorial(N) * math.factorial(M))) % 1000000007)
else:
  print(((math.factorial(N) * math.factorial(M))) % 1000000007)

Solution (Python):
import sys
import collections
import math
from collections import Counter
from collections import deque

N, M = [int(x) for x in input().split()]

D = 10 ** 9 + 7

if abs(N - M) >= 2:
    print(0)
    sys.exit(0)

ret = math.factorial(N) * math.factorial(M)

if N == M:
    ret *= 2


print(ret % D)

Solution (Python):
MOD = 10 ** 9 + 7

def kaijo(n):
    k = 1
    for i in range(1, n + 1):
        k = k * i % MOD
    return k

n, m = map(int, input().split())
if n == m:
    print((kaijo(n) ** 2 * 2) % MOD)
elif abs(n - m) > 1:
    print(0)
else:
    print((kaijo(n) * kaijo(m)) % MOD)

Solution (Python):
MOD = 10 ** 9 + 7

def kaijo(n):
    k = 1
    for i in range(1, n + 1):
        k = k * i % MOD
    return k

n, m = map(int, input().split())
if n == m:
    print((kaijo(n) ** 2 * 2) % MOD)
elif abs(n - m) > 1:
    print(0)
else:
    if n < m:
        print((kaijo(n) ** 2 * m) % MOD)
    else:
        print((kaijo(m) ** 2 * n) % MOD)

Solution (Python):
mod = 10 ** 9 + 7
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
else:
    if N < M:
        N, M = M, N
    ans = 1
    for i in range(1, M + 1):
        ans = ans * i % mod
    if N == M:
        print(ans ** 2 * 2 % mod)
    else:
        print(ans ** 2 * N % mod)

Solution (Python):
n,m = map(int,input().split())
if abs(n - m) >= 2:
  print("0")
else:
  if n == m:cou = 2
  else:cou = 1
  while n > 1:
    cou *= n
    cou %= (10 ** 9 + 7)
    n -= 1
  while m > 1:
    cou *= m
    cou %= (10 ** 9 + 7)
    m -= 1
  print(cou)

Solution (Python):
import sys
N,M=map(int,input().split())
if abs(N-M)>=2:
    print(0)
    sys.exit()
if N<M:
    N,M=M,N
table=[1]*(N+1)
mod=10**9+7
for i in range(1,N+1):
    table[i]=(table[i-1]*i)%mod
if N!=M:
    ans=(table[N]*table[M])%mod
    print(ans)
    sys.exit()
print((2*table[N]*table[M])%mod)

Solution (Python):

n,m = map(int,input().split())
c = [1]

for i in range(1,10**5+10):
    w = (c[-1]*i)%(10**9+7)
    c.append(w)

if m ==n-1:
    print((c[m]*c[n])%(10**9+7))
elif m == n:
    print((c[m]*c[n]*2)%(10**9+7))
elif m == n+1:
    print((c[m]*c[n])%(10**9+7))
else:
    print(0)

Solution (Python):
from sys import exit, setrecursionlimit, stderr
from functools import reduce
from itertools import *
from collections import *
from bisect import *
from heapq import *

def read():
  return int(input())
 
def reads():
  return [int(x) for x in input().split()]

MAXN = 10**6
MOD = 10**9 + 7

fact = [1] * MAXN
for i in range(1, MAXN):
  fact[i] = (fact[i-1] * i) % MOD

N, M = reads()
if abs(N-M) >= 2:
  print(0); exit()

ans = fact[N] * fact[M] * (2 if N == M else 1) % MOD
print(ans)

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

def f(n):
    ans = 1
    for i in range(1,n+1):
        ans*=i
        ans%=MOD
    return ans

sum = 0
if -2<n-m<2:
    if n<m:
        n,m = m,n
    sum = f(n)*f(m)*2**(m-n+1)
    sum %= MOD
print(sum)

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

sum = 0
if -2<n-m<2:
    if n<m:
        n,m = m,n
    sum = math.factorial(n)*math.factorial(m)*2**(m-n+1)
    sum %= 1000000007
print(sum)

Solution (Python):
mod=1000000007
fact=[1 for i in range(100001)]
for i in range(1,100001):
 fact[i]=(fact[i-1]*i)%mod
s=input().split()
n=int(s[0])
m=int(s[1])
n,m=sorted((n,m))
if n==m-1:
 print((fact[n]*fact[m])%mod)
elif n==m:
 print((fact[n]*fact[m]*2)%mod)
else:
 print(0)

Solution (Python):
import math

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

if abs(n-m) >= 2:
    print(0)
elif n == m:
    print(2*math.factorial(n)**2 % 1000000007)
else:
    print(math.factorial(n)*math.factorial(m)%1000000007)

Solution (Python):
import math
N,M=map(int,input().split())
reg=10**9+7











if abs(N-M)>=2:
  print(0)
  exit()
fact=((math.factorial(N)%reg)*(math.factorial(M)%reg))%reg
if abs(N-M)==1:
  print(fact)
else:
  print((2*fact)%reg)

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

MOD = 10**9+7
MAX = max(n, m)+2
fact = [0]*(MAX)
fact[0] = 1
for i in range(1, MAX):
  fact[i] = (fact[i-1]*i)%MOD

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

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

Solution (Python):
a,b=map(int,input().split())
Mod=10**9+7

if abs(a-b)>1:
  print(0)
elif abs(a-b)==1:
  ans=max(a,b)
  for i in range(2,max(a,b)):
    ans*=(i*i)
    ans%=Mod
  print(ans)
else:
  ans=2
  for i in range(2,a+1):
    ans*=(i*i)
    ans%=Mod
  print(ans)

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

if abs(n - m) >= 2:
	print(0)

else:
	n, m = (max(n, m), min(n, m))
	ans = 1
	for i in range(1, m + 1):
		ans = (ans * i) % MOD

	if n == m:
		print((ans ** 2 * 2) % MOD)
	else:
		print((ans ** 2 * n) % MOD)

Solution (Python):
mod = 1000000007

N,M = map(int,input().split())

if abs(N-M)>1:
    print(0)
else:
    ans = 1
    for i in range(1,N+1): ans = (ans*i)%mod
    for j in range(1,M+1): ans = (ans*j)%mod
    if N==M: ans = (ans*2)%mod
    print(ans)

Solution (Python):
l = map(int, raw_input().split())
n = l[0]
m = l[1]
mod = 1000000000 + 7

'''
def mod_pow(x,n,mod): 
    ll ret = 1;
    while n > 0:
        if n & 1 :
            (ret *= x) %= mod
        (x *= x) %= mod
        n /= 2
    return ret

def combo(n,k,mod):
    if n / 2 < k:
        k = n - k
    bunshi = 1
    for i in range(n - k + 1,n + 1):
        bunshi *= i;
        bunshi %= mod;
    bunbo = 1
    for i in range(1,k + 1):
        bunbo *= i;
        bunbo *= mod;

    (bunshi *= mod_pow(bunbo(bunshi,mod - 2,mod)) %= mod)
'''

def f(n):
    ret = 1
    for i in  range(1,n + 1) :
        ret *= i
        ret %= mod
    return ret

if m < n :
    l = n
    n = m
    m = l

if n == m :
    print (2 * f(n) * f(n) % mod)
elif m - n < 2 :
    print (f(n) * f(m) % mod)
else:
    print (0)

Solution (Python):
import math
PRIME_NUM = 1000000007
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
elif N == M:
    print(2 * (math.factorial(N) % PRIME_NUM) * (math.factorial(M) % PRIME_NUM) % PRIME_NUM)
else:
    print((math.factorial(N) % PRIME_NUM) * (math.factorial(M) % PRIME_NUM) % PRIME_NUM)

Solution (Python):

import math

line = input().split(" ")
n = int(line[0])
m = int(line[1])

def perm(x):
    return math.factorial(x)

p = 10**9 + 7

f = 1
if n==m:
    f = 2

if abs(n-m)>1:
    print(0)
else:
    print(f*perm(n)*perm(m)%p)

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

Solution (Python):
import math
n,m=map(int,input().split())
ans=1
for i in range(1,n+1):
    ans=ans*i%1000000007
for i in range(1,m+1):
    ans=ans*i%1000000007

if abs(n-m)>1:
    print(0)
elif n!=m:
    print(ans)
else:
    print(ans*2%1000000007)

Solution (Python):


N, M = map(int, input().split())

min_num = min(N, M)
max_num = max(N, M)
diff = abs(N-M)
K = 10**9 + 7

if diff >= 2:
    print(0)
    exit()

ans = 0
if diff == 1:
    ans = 1
else:
    ans = 2

for i in range(N):
    ans = ((i+1)*ans) % K

for i in range(M):
    ans = ((i+1)*ans) % K

print(ans)

Solution (Python):
import math
N, M = map(int,input().split())

res = 0
if N == M:
	res = (2 * math.factorial(N) * math.factorial(M)) % (10**9 + 7)
elif N == M - 1 or N == M + 1:
	res = (math.factorial(N) * math.factorial(M)) % (10**9 + 7)
else:
	res = 0

print(res)

Solution (Python):

import math
import sys
 
n, m = map(int, input().split())
 
if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0
 
print(math.factorial(n) * math.factorial(m) * k % 1000000007)

Solution (Python):

import array
def get_fact(n, mod):
    fact = array.array('l')
    fact.append(1)
    for i in range(n):
        fact.append((i + 1) * fact[-1] % mod)
    return fact

mod = 10 ** 9 + 7
n, m = map(int, input().split())
fact = get_fact(max(n, m), mod)
if n == m:
    result = (fact[n] * fact[m]) * 2
elif abs(n - m) == 1:
    result = fact[n] * fact[m]
else:
    result = 0
print(result % mod)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, factorial
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10
MOD = 10 ** 9 + 7

def main():
    N, M = map(int, input().split())
    diff = abs(N - M)
    if diff > 1:
        print(0)
        return

    ans = 2 if diff == 0 else 1
    for i in range(1, N + 1):
        ans = (ans * i) % MOD
    for i in range(1, M + 1):
        ans = (ans * i) % MOD
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import math

n,m=map(int,raw_input().split())
res=((math.factorial(m)%(10**9+7))*math.factorial(n)%(10**9+7))%(10**9+7)
if abs(n-m)==1:
 print res
elif n==m:
 print (res*2)%(10**9+7)
else:
 print 0

Solution (Python):
def reconciled():
	n, m = map(int, raw_input().split(" "))
	if abs(n - m) > 1:
		print 0
		return
	mod = 10 ** 9 + 7
	mod_n = 1
	mod_m = 1
	for i in reversed(range(1, n + 1)):
		mod_n = (mod_n * i) % mod
	for j in reversed(range(1, m + 1)):
		mod_m = (mod_m * j) % mod
	if n != m:
		print (mod_n * mod_m) % mod
	else:
		print (2 * mod_n * mod_m) % mod


if __name__ == "__main__":
	reconciled()

Solution (Python):

N, M = map(int, input().split())


mod = 10 ** 9 + 7
if N > M:
    N, M = M, N

if M - N >= 2:
    print(0)
elif M - N == 1:
    prod = 1
    for i in range(1, N + 1):
        prod = (prod * i) % mod
    print((prod * prod * M) % mod)
else:
    prod = 1
    for i in range(1, N + 1):
        prod = (prod * i) % mod
    print((2 * prod * prod) % mod)

Solution (Python):
N, M = map(int, input().split())



mod = 10 ** 9 + 7
if abs(N - M) >= 2:
    print(0)
else:
    if N == M:
        prod = 1
        for i in range(2, N + 1):
            prod = (prod * i) % mod
        print((prod * prod * 2) % mod)
    else:
        if N < M:
            N, M = M, N
        prod = 1
        ans = 1
        for i in range(2, N + 1):
            prod = (prod * i) % mod
            if i == M:
                ans = (ans * prod) % mod
        print((ans * prod) % mod)

Solution (Python):
import math as mt

a,b=map(int,input().split())
if a==b:print(2*mt.factorial(a)**2%1000000007)
elif mt.fabs(a-b)==1:print(mt.factorial(a)*mt.factorial(b)%1000000007)
else :print(0)

Solution (Python):
import sys
import math
n, m = (int(i) for i in input().split())
l=abs(n-m)
div=int(10**9+7)

if l>1:
    print(0)
    sys.exit()

if l==1:
    ans=(math.factorial(n)*math.factorial(m))%div
    print(ans)
else:
    ans=(math.factorial(n)*math.factorial(m)*2)%div
    print(ans)

Solution (Python):
def fac(n, MOD):
    cur = 1
    for i in range(1, n+1):
        cur = (cur*i)%MOD
    return cur

MOD = 10**9+7
n, m = map(int,input().split())
if abs(n-m) >= 2: print(0)
elif n == m: print((2*fac(n,MOD)**2)%MOD)
else: print(fac(n,MOD)*fac(m,MOD)%MOD)

Solution (Python):


def main():
    N,M=map(int,input().split())
    if(abs(N-M)>=2):
        print(0)
        return

    ret=1
    mod=10**9+7
    for i in range(1,N+1):
        ret*=i
        ret%=mod
    for i in range(1,M+1):
        ret*=i
        ret %= mod
    if(N==M):
        r=2
    else:
        r=1
    print((ret*r)%mod)


if __name__ == '__main__':
    main()

Solution (Python):
mod=10**9+7
n,m=map(int,raw_input().split())
mul1=mul2=1
if abs(n-m)>1:
    print 0
else:
    for i in range(1,n+1):
        mul1*=i
        mul1%=mod
    for i in range(1,m+1):
        mul2*=i
        mul2%=mod
    if n==m:
        print (2*mul1*mul2)%mod
    else:
        print (mul1*mul2)%mod

Solution (Python):
MODULO = 10**9+7

def perm(n):
	p = 1
	for i in range(1,n+1):
		p = (p*i) % MODULO 
	return p

n, m = map(int, input().strip().split())
x = abs(n-m)
if (x > 1):
	print(0)
else:
	permn = perm(n)
	permm = perm(m)

	if x==0: 
		print((permn*permm*2)%MODULO)
	else: 
		print((permn*permm)%MODULO)

Solution (Python):
from math import *
M,N=input().split(' ')
M=int(M)
N=int(N)

res=1
if M==N:
    res=factorial(M)
    res=res*res*2
elif M-N==1:
    res=factorial(N)
    res*=res*M
elif M-N==-1:
    res=factorial(M)
    res*=res*N
else:
    res=0

print(res%(10**9+7))

Solution (Python):
answer = 0
mod = 10**9 + 7
a,b = (int(n) for n in input().split(" "))
if abs(a-b) <= 1:
    temp1 = 1
    for i in range(1,a+1):
        temp1 = (temp1 * i) % mod
    temp2 = 1
    for i in range(1,b+1):
        temp2 = (temp2 * i) % mod
    answer += (temp1 * temp2) % mod
    if (a + b) % 2 == 0:
        answer = (answer * 2) % mod
    print(answer)
else:
    print(0)

Solution (Python):

import math
import sys

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

if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0

print(math.factorial(n) * math.factorial(m) * k % 1000000007)

Solution (Python):

import math
import sys

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

if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0

print(math.factorial(n) * math.factorial(m) % 1000000007 * k % 1000000007)

Solution (Python):

import math
import sys

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

if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0

print(math.factorial(n) % 1000000007 * math.factorial(m) % 1000000007 * k % 1000000007)

Solution (Python):


d = 10**9 + 7
n, m = map(int, input().split(' '))

ans = 0

ans1 = 1
n1 = n
m1 = m
while n1 >= 1:
    ans1 *= n1 * m1
    ans1 %= d
    n1 -= 1
    m1 -= 1

if m1 > 1:
    ans1 = 0

ans2 = 1
n2 = n
m2 = m
while m2 >= 1:
    ans2 *= m2 * n2
    ans2 %= d
    n2 -= 1
    m2 -= 1

if n2 > 1:
    ans2 = 0


print((ans1 + ans2) % d)

Solution (Python):

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

Solution (Python):
import sys

N, M = (int(i) for i in raw_input().split())
ans = 1                                                                                                                                                                                                                                                                     

if __name__ == '__main__':
    if N == (M + 1) or N == (M - 1):
        for n in range(1, N+1):
            ans *= n
            ans %= 1e9 + 7
        for m in range(1, M+1):
            ans *= m
            ans %= 1e9 + 7
    elif N == M:
        for n in range(1, N+1):
            ans *= n
            ans %= 1e9 + 7
        for m in range(1, M+1):
            ans *= m
            ans %= 1e9 + 7
        ans *= 2
        ans %= 1e9 + 7
    else:
        ans = 0

    print int(ans)

Solution (Python):
from math import factorial


def solve(n, m):
    d = abs(n - m)
    if d > 1:
        return 0

    DIV = int(1e9 + 7)
    if d == 1:
        l = min(n, m)
        f = factorial(l) % DIV
        return f ** 2 * (l + 1) % DIV
    f = factorial(n) % DIV
    return f ** 2 * 2 % DIV


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

Solution (Python):
buf=input().split()
N=int(buf[0])
M=int(buf[1])

thres=10**9+7

if abs(N-M)==1:
    N=min(N,M)
    retVal=1
    for i in range(1,N+1):
        retVal*=(i**2)
        if retVal>=thres:
            retVal=retVal%thres
    retVal*=(N+1)
    if retVal>=thres:
        retVal=retVal%thres
    print(retVal) 
elif N==M:
    retVal=2
    for i in range(1,N+1):
        retVal*=(i**2)
        if retVal>=thres:
            retVal=retVal%thres
    print(retVal)
else:
    print(0)

Solution (Python):
N,M = map(int,input().split())
MOD = 10**9+7

if abs(N-M) <= 1:
  cnt = 2 - abs(N-M)
  for i in range(1,N+1):
    cnt *= i
    cnt %= MOD

  for i in range(1,M+1):
    cnt *= i
    cnt %= MOD
else:
  cnt = 0

print(cnt)

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

mod=10**9+7

def kaijo(n):
    result=1
    for i in range(2,n+1):
        result*=i
        if result>mod:
            result=result%mod
    return result
    
def count(n,m):
    return kaijo(n)*kaijo(m)

if n-m==-1 or n-m==1:
    result=count(n,m)
    amari=result%mod
    print amari
elif n-m==0:
    result=count(n,m)
    result*=2
    amari=result%mod
    print amari
else:
    print 0

Solution (Python):
from functools import reduce

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

mod = 1000000007

def factorial(n):
    return reduce(lambda x, y: x*y%mod, range(1, n+1))

if n==m:
    print(2*factorial(n)*factorial(m)%mod)
elif abs(n-m)<=1:
    print(factorial(n)*factorial(m)%mod)
else:
    print(0)

Solution (Python):
n,m=map(int,input().split())
M=10**9+7
def w(i,r=1):
 while i:r=r*i%M;i-=1
 return r
print(max(0,(2-abs(n-m))*w(n)*w(m))%M)

Solution (Python):

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

Solution (Python):
MOD = pow(10, 9)+7
N, M = map(int, raw_input().split())

def myfact(num):
	res = 1
	for i in range(num+1)[1:]:
		res *= i
		if res > MOD:
			res %= MOD
	return res
if abs(N - M) > 1:
	print 0
else:
	if N == M:

		res = ( myfact(N) * myfact(N) * 2 )%MOD
	else:
		res = ( myfact(N) * myfact(M) )%MOD
	print res

Solution (Python):
dog_ape = list(map(int,input().split(" ")))
dog_ape.sort()
cut     = (10**9)+7

def kai(n):
    ans = 1
    r   = 0
    for i in range(n):
        ans *= (i+1)**2
        if ans > cut:
            ans %= cut
    return ans

if dog_ape[0] == dog_ape[1]:
    ans = ( kai(dog_ape[0]) )*2
    ans %= cut
    print(ans)

elif dog_ape[1] - dog_ape[0] == 1:
    ans = kai(dog_ape[0])*(dog_ape[1])
    ans %= cut
    print(ans)

else:
    print(0)

Solution (Python):
stan = 10**9+7
def pow(x):
    start = 1
    for i in range(1,x+1 ):
        start = start * i % stan
    return start
n,m = [int(x) for x in input().split(' ')]
n, m = max([n,m]), min([n,m])
if n - m > 1:
    print(0)
    exit()
if n - m == 1:
    sum = pow(n) * pow(m)
    print(sum % stan)
elif n == m:
    sum = pow(n) * pow(m)
    print(2 * sum % stan)

Solution (Python):
import math
n, m = [int(i) for i in input().split()]
print(0 if abs(n-m) > 1 else (math.factorial(n) * math.factorial(m) * (2 if n == m else 1)) % int(1e9 + 7))

Solution (Python):
import math
max = 1000000007
def test(x):
  return x if x<max else (x % max)

def kai(m):
  if m == 1:
    return 1
  else:
    return test(kai(m-1) * m)

n,m = map(int,input().split())
ret = 0
if n == m:
  k = math.factorial(m)
  ret = test(2 * k * k)
elif n-m == 1:
  k = math.factorial(m)
  ret = test(k * k * n)
elif n-m == -1:
  k = math.factorial(n)
  ret = test(k * k * m)
print(ret)

Solution (Python):
import sys
import math

def fact(n, modulus):
    ans=1
    for i in range(1,n+1):
        ans = ans * i % modulus    
    return ans % modulus

arr=[]
arr =(input().split())

n = int(arr[0])
m = int(arr[1])

modulus = 1000000007

if abs(n-m)>1 :
    print(0)
elif n==m:
    ans = (fact(n,modulus)*fact(m,modulus))%modulus
    ans = ans+ans
    if ans > modulus:
        ans-=modulus
    print(ans)
else:
    print( (fact(n,modulus)*fact(m,modulus))%modulus)

Solution (Python):
import math
import sys

a = sys.stdin.readline().split(' ')
n = int(a[0])
m = int(a[1])

modulator = 1000000007

if n == m:
	print((math.factorial(n) * math.factorial(m) * 2) % modulator)
elif abs(n - m) == 1 :
	print((math.factorial(n) * math.factorial(m)) % modulator)
else:
	print(0)

Solution (Python):
import math
N, M = input().split()
N = int(N)
M = int(M)

if abs(N - M) > 1:
    print(0)
elif N == M:
    res = math.factorial(N) * math.factorial(M) * 2
    print(res % int(1e9 + 7))
else:
    res = math.factorial(N) * math.factorial(M)
    print(res % int(1e9 + 7))

Solution (Python):


import math

MAX = 1000000007

def ans(m,n):
    if ( abs(m - n) > 1 ):
        print(0);
        return 0

    if ( abs(m - n) == 1 ):
        print((math.factorial(m)*math.factorial(n))%MAX);
        return 0
    
    if ( abs(m - n) == 0 ):
        print((2*(math.factorial(m)*math.factorial(n)))%MAX);
        return 0

m,n = [int(i) for i in input().split()]
ans(m,n)

Solution (Python):

import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N, M = ILI()
    return N, M


def solve(N, M):
    if abs(N - M) >= 2:
        return 0
    elif abs(N - M) == 1:
        num_min = min(N, M)
        ans = ((math.factorial(num_min) ** 2) * (num_min + 1)) % MOD
        return ans
    elif abs(N - M) == 0:
        ans = (2 * (math.factorial(N) ** 2)) % MOD
        return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]), int(input_raw[1])]

    dog_ord_num = 1
    mon_ord_num = 1

    if abs(N - M) <= 1:
        for i in range(N):
            dog_ord_num *=  i + 1
            dog_ord_num = dog_ord_num % (int(1e+9) + 7)

        for i in range(M):
            mon_ord_num *= i + 1
            mon_ord_num = mon_ord_num % (int(1e+9) + 7)



        all_ord = (dog_ord_num * mon_ord_num ) % (int(1e+9) + 7)
        if (abs(N - M) == 0):
            all_ord *= 2
            all_ord %= (int(1e+9) + 7)

    else:
        all_ord = 0

    print int(all_ord)


if __name__ == '__main__':
    main()

Solution (Python):
def factorial(n):
    x = n
    r = 1
    while x != 1:
        r *= x
        r %= d
        x -= 1
    return r

d = (10**9) + 7
n, m = map(int, raw_input().split())
if not(n-1 <= m <= n+1):
    print 0
    exit()
if n == m:
    c = factorial(n)**2 * 2 % d
else:
    c = factorial(n) * factorial(m) % d
print c

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

mod = int(1e9) + 7

if n > m:
  n, m = m, n

def p(n):
  ret = 1
  for i in xrange(1, n + 1):
    ret = ret * i % mod
  return ret
    

def solve(n, m):
  if n == m:
    return p(n) * p(m) * 2 % mod
  else:
    return p(n) * p(m) % mod

if n == m or n + 1 == m:
  print(solve(n, m))
else:
  print(0)

Solution (Python):
N, M = map(int, raw_input().split())

fact = [0 for _ in range(100000+1)]
fact[0] = 1
for i in xrange(1, 100000+1):
    fact[i] = (fact[i-1] * i) % (10**9 + 7)

ans = 0
if N == M:
    ans += fact[N] * fact[M]
    ans += fact[M] * fact[N]
if N == M - 1 or M == N - 1:
    ans += fact[N] * fact[M]

ans %= (10**9 + 7)
print ans

Solution (Python):

def Factorial(integer) :
    global M
    product = 1
    if integer == 0 : return 1

    for num in range(1 ,integer + 1):
        product *= num
        product%=M
    return product%M


d=sorted(list(map(int,input().split(" "))))
M=10**9+7

if abs(d[0]-d[1])>1:
    res=0
elif d[0]==d[1]:

    res=((Factorial(d[0])%M)**2)*2
else:
    res=Factorial(d[1])*Factorial(d[0])

print(res%(M))

Solution (Python):
import math

N, M = map(int, input().split())

if N == M:
    k = math.factorial(N)
    count = k * k * 2 % (10**9 + 7)
    print(count)
elif N + 1 == M:
    k = math.factorial(N)
    count = k * (k * M) % (10**9 + 7)
    print(count)
elif N == M + 1:
    k = math.factorial(M)
    count = k * (k * N) % (10**9 + 7)
    print(count)
else:
    print(0)

Solution (Python):
read = lambda: map(int, input().split())
mod = 10 ** 9 + 7
n, m = read()
if abs(n - m) > 1:
    print(0)
    exit()
N = max(n, m) + 1
fac = [0] * N
fac[0] = 1
for i in range(1, N):
    fac[i] = (fac[i - 1] * i) % mod
if n == m:
    ans = (2 * fac[n] * fac[m]) % mod
else:
    ans = fac[n] * fac[m] % mod
print(ans % mod)

Solution (Python):
import math
f=math.factorial
n,m=map(int,input().split())
if abs(n-m)>1:
    print("0")
elif abs(n-m)==1:
    ans=f(n)*f(m)
    ans%=1000000007
    print(ans)
else:
    ans=f(n)*f(m)
    ans*=2;
    ans%=1000000007
    print(ans)

Solution (Python):

N, M = map(int, input().split())
L = 1000000007

res = 1

if (M - N) >= 2 or (N - M) >= 2:
    res = 0
else:
    for k in range(M):
        res *= (k+1)
        res = res % L
    for k in range(N):
        res *= (k+1)
        res = res % L
    if M == N:
        res *= 2
        res = res % L
print(res)

Solution (Python):
from math import factorial

N,M = map(int,input().split())

def foo(N,M):
	if N == M+1 or N+1 == M:
		tm = factorial(min(N,M))
		return(tm*max(N,M)*tm)

	elif N==M:
		return((factorial(N)**2)*2)

	else:
		return(0)

print(foo(N,M)%(10**9+7))

Solution (Python):
import math
fact = math.factorial
n, m = [int(i) for i in input().split()]

def dog_monk(n,m):
    diff = abs(n-m)
    if diff > 1:
        return 0
    elif diff == 1:
        return fact(m) * fact(n)
    elif diff == 0:
        return 2 * fact(m) * fact(n)

print(dog_monk(n,m) % (10 ** 9 + 7))

Solution (Python):
from math import factorial
n, m = [int(i) for i in input().split()]
const = 10**9 + 7
nf = factorial(n) % const
mf = factorial(m) % const
ret = (nf * mf) % const
if n - m == 1 or m - n == 1:
    ret = (nf * mf) % const
elif n == m:
    ret *= 2
    ret %= const
else:
    ret = 0
print(ret)

Solution (Python):
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))


N, M = rli()
ans = 1
mod = 1000000007
if(abs(N-M)>=2):
    ans = 0
else:
    for i in range(1, N+1):
        ans = (ans*i) % mod
    for i in range(1, M+1):
        ans = (ans*i) % mod
    if(N == M):
        ans = (ans*2) % mod

print(ans)

Solution (Python):
from math import *
a,b=map(int,input().split())
if a==b:
    print((2*factorial(a)*factorial(b)%(10**9+7)))
elif abs(a-b)==1:
    print((factorial(a)*factorial(b)%(10**9+7)))
else:
    print(0)

Solution (Python):
def factorial(x):
    res = 1
    for i in range(1, x):
        res *= i + 1
        res %= 10 ** 9 + 7
    return res

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

Solution (Python):
import sys

def main():
    line = sys.stdin.readline()
    lst = line.split(' ')
    a = int(lst[0])
    b = int(lst[1])
    n = min(a,b)
    m = max(a,b)

    if (m == n+1):
    

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

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

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

        for i in range(1,m+1):
            ans = (ans * i) % 1000000007
        print(ans)
    else:
        print(0)
    return 0

if __name__ == '__main__':
    sys.exit(main())

Solution (Python):

from math import factorial

N, M = [int(n) for n in input().split()]
if abs(N-M) == 0:
    result = factorial(N)**2 * 2
elif abs(N-M) == 1:
    result = factorial(N) * factorial(M)
else:
    result = 0
    
print(result % (10**9 + 7))

Solution (Python):
MOD = 1000000007
MAX = 100005
P = [0 for i in range(MAX)]
P[0] = 1
for i in range(MAX-1):
    P[i+1] = P[i] * (i + 1)
    P[i+1] %= MOD

N, M = map(int, input().split())

if N == M:
    res = 2 * P[N] * P[N]
elif abs(N - M) == 1:
    res = P[N] * P[M]
else:
    res = 0
print(res % MOD)

Solution (Python):
MOD = int(1e9) + 7


def f(n):
    ans = 1
    for i in range(1, n + 1):
        ans *= i
        ans %= MOD
    return ans


N, M = map(int, input().split())

if abs(N - M) >= 2:
    print(0)
    exit()

maxv = max(N, M)
minv = min(N, M)

if maxv == minv:
    ans = f(maxv)
    ans *= f(minv)
    ans *= 2
    ans %= MOD
    print(ans)
    exit()

ans = f(maxv)
ans *= f(minv)
ans %= MOD
print(ans)

Solution (Python):
import math
p = 1000000007
if __name__ == '__main__':
    n,m = list(map(int,input().split()))
    if n==m:
        print(((math.factorial(n)%p)*(math.factorial(n)%p)*2)%p)
    elif abs(n-m)==1:
        print((math.factorial(m)%p)*(math.factorial(n)%p)%p)
    else:
        print(0)

Solution (Python):
temp=input().split()
N=int(temp[0])
M=int(temp[1])
ans=0
INF=1000000000

if abs(N-M)==0:
	ans=2
elif abs(N-M)==1:
	ans=1
else:
	ans=0


for i in range(1,N+1):
	ans*=i
	ans%=INF+7
for i in range(1,M+1):
	ans*=i
	ans%=INF+7
print(ans)

Solution (Python):
import sys



N = 10 ** 5
M = 10 ** 9 + 7


def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def solve(n, m):
    if abs(n - m) > 1:
        return 0
    res = f[m] * f[n]
    res %= M
    if m == n:
        res *= 2
        res %= M
    return res


f = [1] * (N + 1)
for i in range(1, N + 1):
    f[i] = i * f[i - 1]
    f[i] %= M

n, m = read_int_list()
res = solve(n, m)
print(res)

Solution (Python):
N, M = map(int, input().split())

def fac(n, mod):
    a = 1
    for i in range(n, 0, -1):
        a = (a * i) % mod
    return a
    
def solve(N, M):
    mod = 10**9 + 7
    if N > M + 1 or M > N + 1:
        return 0
    if N == M:
        return (2 * fac(N, mod)**2) % mod
    else:
        return (fac(N, mod) * fac(M, mod)) % mod

print(solve(N, M))

Solution (Python):



import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

MOD = 1000000007
def func(n):
    ret = 1
    for i in range(n):
        ret *= (i + 1)
        ret %= MOD
    return ret

N, M = map(int, input().split())
if N == M:
    print((2 * func(N)**2) % MOD)
elif abs(N - M) == 1:
    print((func(N) * func(M)) % MOD)
else:
    print(0)