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 sys
from math import factorial

def main():
    input = sys.stdin.readline
    N, M = map(int, input().split())
    MOD = 10**9 + 7

    if N == M:
        return (2 * factorial(N) * factorial(M)) % MOD
    elif abs(N - M) == 1:
        return (factorial(N) * factorial(M)) % MOD
    else:
        return 0


if __name__ == '__main__':
    print(main())

Solution (Python):
import itertools
import math
from collections import deque
from collections import defaultdict
from itertools import permutations
import heapq
import bisect
from scipy.sparse.csgraph import floyd_warshall as wf
INF = float("inf")
mod = 10**9+7
n,m = map(int,input().split())
def factorial(x):
    ans = 1
    for i in range(1,x+1):
        ans =ans*i%mod
    return ans


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

Solution (Python):
N, M = list(map(int, input().split()))
if abs(N-M)>1:
  print(0)
  exit()

mod = 1000000007

def modfact(N):
  answer=1
  while N>=2:
    answer=(answer*N)%mod
    N-=1
  return answer

if (N-M)%2==0:
  print((2*modfact(N)**2)%mod)
else:
  print((modfact(min(N,M))**2*max(N,M))%mod)

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

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

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))%int(1e9+7))
else:
  print((math.factorial(N)*math.factorial(M))%int(1e9+7))

Solution (Python):
import math

N, M = map(int, input().split())
a = 1
if N == M:
    print(((math.factorial(N) % (10 ** 9 + 7)) ** 2 * 2) % (10 ** 9 + 7))
elif N == M + 1 or N + 1 == M:
    M, N = max(N, M), min(N, M)
    print(((math.factorial(N) % (10 ** 9 + 7)) ** 2 * M) % (10 ** 9 + 7))
else:
    print(0)

Solution (Python):
import math

N, M = map(int, input().split())
a = 1
if N == M:
    
    
    
    
    
    
    print(((math.factorial(N) % (10 ** 9 + 7)) ** 2 * 2) % (10 ** 9 + 7))
elif N == M + 1 or N + 1 == M:
    M, N = max(N, M), min(N, M)
    
    
    
    print(((math.factorial(N) % (10 ** 9 + 7)) ** 2 * M) % (10 ** 9 + 7))
else:
    print(0)

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

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))%int(1e9+7))
else:
  print((math.factorial(N)*math.factorial(M))%int(1e9+7))

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

Solution (Python):
def Factorial(A):
    MOD = 10**9+7
    X = 1
    for num in range(1, A+1):
        X *= num
        X %= MOD
    return X

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

if n == m+1:
    ans = Factorial(n)*Factorial(m)
elif n == m:
    ans = Factorial(n)*Factorial(m)*2
elif n+1 == m:
    ans = Factorial(n)*Factorial(m)
else:
    ans = 0
print(ans%MOD)

Solution (Python):
def fact_mod(n: int, mod: int)->int:
    ret = 1
    while n > 0:
        ret = (ret * n) % mod
        n -= 1
    return ret


def reconciled(N: int, M: int)->int:
    MOD = 10**9 + 7
    if abs(N - M) > 1:
        return 0
    if abs(N - M) == 1:
        return (fact_mod(N, MOD) * fact_mod(M, MOD)) % MOD

    return (2 * fact_mod(N, MOD) * fact_mod(M, MOD)) % MOD


if __name__ == "__main__":
    N, M = map(int, input().split())
    ans = reconciled(N, M)
    print(ans)

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

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

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

Solution (Python):
N,M = map(int,input().split())
MOD = 10**9+7
ans = 1
if abs(N-M) > 1:
    ans = 0
else:
    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,input().split())
MOD = 10**9+7
ans = 1
if abs(N-M) > 1:
    ans = 0
else:
    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):
import math

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

if n==m+1 or n==m-1:
    print((math.factorial(m)*math.factorial(n))%(10**9+7))
elif n==m:
    print((2*(math.factorial(m)**2))%(10**9+7))
else:
    print(0)

Solution (Python):
import math
N,M = map(int, input().split())
a = abs(N - M)
amari = (7 + (10**9))
if N < M:
    N,M = M,N
if a == 0:
    nd = math.factorial(N)
    print( ((nd**2)*2) % amari)

elif a == 1:
    md = math.factorial(M)
    nd = md * N
    print((md * nd) % amari)
else:
    print(0)

Solution (Python):
import math

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

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

Solution (Python):
import math
n = sorted(list(map(int,input().split())))
n[1] %= (10**9 + 7)
n[0] %= (10**9 + 7)
if n[1] - n[0] >= 2:
    ans = 0
elif n[1] - n[0] == 0:
    ans = (math.factorial(n[1])%(10**9 + 7)) **2 * 2
else:
    ans = n[1] * (math.factorial(n[0]) % (10**9 + 7))**2
print(ans % (10**9 + 7) )

Solution (Python):
import math
n = sorted(list(map(int,input().split())))
n[1] %= (10**9 + 7)
n[0] %= (10**9 + 7)
if n[1] - n[0] >= 2:
    ans = 0
elif n[1] - n[0] == 0:
    ans = math.factorial(n[1]) **2 * 2
else:
    ans = n[1] * math.factorial(n[0])**2
print(ans % (10**9 + 7) )

Solution (Python):
import math
n = sorted(list(map(int,input().split())))
if n[1] - n[0] >= 2:
    ans = 0
elif n[1] - n[0] == 0:
    ans = math.factorial(n[1]) **2 * 2
else:
    ans = n[1] * math.factorial(n[0])**2
print(ans % (10**9 + 7) )

Solution (Python):
import math
n = sorted(list(map(int,input().split())))
if n[1] - n[0] >= 2:
    ans = 0
elif n[1] - n[0] == 0:
    ans = math.factorial(n[1]) **2 * 2
else:
    ans = math.factorial(n[1]) * math.factorial(n[0])
print(ans % (10**9 + 7) )

Solution (Python):
import math
n = sorted(list(map(int,input().split())))
if n[1] - n[0] >= 2:
    ans = 0
elif n[1] - n[0] == 0:
    ans = math.factorial(n[1]) * math.factorial(n[0]) * 2
else:
    ans = math.factorial(n[1]) * math.factorial(n[0])
print(ans % (10**9 + 7) )

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

import math
if abs (N - M) > 1:
    print(0)
else:
    if (N + M) % 2 == 0:
        print(math.factorial(N) * math.factorial(M) * 2 % (10 ** 9 + 7))
    else:
        print(math.factorial(N) * math.factorial(M) % (10 ** 9 + 7))

Solution (Python):
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
n = 1
for i in range(1, N+1):
    n *= i
    n %= MOD

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

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

Solution (Python):
n,m=map(int,input().split())
if abs(n-m)>=2:
  print(0)
else:
  a=1
  if n==m:
    for i in range(2,n+1):
      a*=i
      a%=1000000007
    print((a*a*2)%1000000007)
  elif n<m:
    for i in range(2,n+1):
      a*=i
      a%=1000000007
    b=(a*m)%1000000007
    print((a*b)%1000000007)
  else:
    for i in range(2,m+1):
      a*=i
      a%=1000000007
    b=(a*n)%1000000007
    print((a*b)%1000000007)

Solution (Python):
n,m = map(int,input().split())
a = 10**9+7
if m > n:
    n,m = m,n
if not (0 <= n-m <= 1):
    print(0)
    exit()
 
if n == m:
    num = 2
    n = 1
else:
    num = 1
t = 1
for i in range(1,m+1):
    t *= i
    t %= a
print(num*t*t*n%a)

Solution (Python):
import math
n,m = map(int,input().split())
a = 1000000007
if not (0 <= abs(n-m) <= 1):
    print(0)
    exit()

if n == m:
    num = 2
else:
    num = 1
print(num*math.factorial(n)*math.factorial(m) % a)

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

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


MOD = 1000000007

ans = 0
if abs( N - M ) <= 1: 
    if N == M: ans = ( N + M ) * N
    else:      ans = N * M
    
    while N > 1 and M > 1:
            ans *= ( N - 1 ) * ( M - 1 ) % MOD
            ans %= MOD
            N -= 1
            M -= 1

print( ans )

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


MOD = 1000000007
if N < M: N, M = M, N  

ans = 0
if N - M <= 1: 
    if N == M: ans = ( N + M ) * N
    else: ans = N * M
    
    while N > 1 and M > 1:
            ans *= ( N - 1 ) * ( M - 1 ) % MOD
            ans %= MOD
            N -= 1
            M -= 1

print( ans )

Solution (Python):
import math
n,m = (int(i) for i in input().split())
if abs(n-m) > 1:
    print(0)
    exit()

res = math.factorial(n) * math.factorial(m)
if n==m:
    res *= 2
print(res%(10**9+7))

Solution (Python):
from math import factorial

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

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

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

Solution (Python):





n, m = map(int, input().split())
if n == m:
    ans = 2  
    for i in range(2, n+1):
        ans *= i
        ans %= 10**9+7
        ans *= i
        ans %= 10**9+7
    print(ans)
elif abs(n - m) == 1:
    ans = 1
    for i in range(2, n+1):
        ans *= i
        ans %= 10**9+7
    for i in range(2, m+1):
        ans *= i
        ans %= 10**9+7
    print(ans)
else:
    print(0)

Solution (Python):
N,M=map(int,input().split())
def bikkuri(A):
    ans=1
    for i in range(A):
        ans*=i+1
        ans%=1000000007
    return ans

if N==M:
    p=2*(bikkuri(N)*bikkuri(M)%1000000007)
    p%=1000000007
elif  abs(N-M)==1:
    p=(bikkuri(M)*bikkuri(N)%1000000007)
    p%=1000000007
else:
    p=0
print(p)

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

if abs(N-M)>=2:
  print 0
else:
  mod = 10**9+7
  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):
MOD = 10**9+7


def main():
    N, M = map(int, input().split())
    if abs(N-M) >= 2:
        print(0)
        return
    F = [1] * (max(N, M) + 1)
    for i in range(2, max(N, M) + 1):
        F[i] = F[i-1] * i % MOD
    if N == M:
        print((F[N] * F[M] * 2) % MOD)
    else:
        print((F[N] * F[M]) % MOD)


if __name__ == "__main__":
    main()

Solution (Python):
A, B = map(int,input().split())
mod = 10**9+7
def factorical(n):
  ret = 1
  for i in range(1,n+1):
    ret *= i
    ret %= mod
  return ret%mod

a = factorical(A)
b = factorical(B)
if(abs(A-B) >= 2):
  print(0)
elif(A == B):
  print(a*b*2%mod)
else:
  print(a*b%mod)

Solution (Python):
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
input = sys.stdin.readline
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N,M = IL()

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

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

print(ans)

Solution (Python):
import math
def f(x):
    return math.factorial(x) % (10**9 + 7)

a,b = map(int,input().split())

if abs(a-b) <= 1:
    print((f(a)*f(b)) % (10**9 + 7) if abs(a-b) == 1 else (2*f(a)*f(b)) % (10**9 + 7))
else:
    print(0)

Solution (Python):

import sys

def main():
	N, M = map(int, input().split(" "))
	
	if N - M >= 2 or M - N >= 2:
		print(0)
		sys.exit()
		
	A_ans = 1
	for i in range(1, M+1):
		A_ans = (A_ans * i) % 1000000007
		
	B_ans = 1
	for i in range(1, N+1):
		B_ans = (B_ans * i) % 1000000007
	
	ans = (A_ans * B_ans) % 1000000007
	if N == M:
		ans = (ans * 2) % 1000000007
		
	print(ans)
		
if __name__ == "__main__":
	main()

Solution (Python):
M = 1000000007

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

def solve(n, m):
    d = abs(n-m)

    if d > 1:
        return 0
    elif d == 1:
        return fact(n) * fact(m) % M
    else:
        return fact(n) * fact(m) * 2 % M

def fact(n):
    s = 1
    for i in range(2, n+1):
        s = (s * i) % M
    return s

main()

Solution (Python):
M = 1000000007

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

def solve(n, m):
    d = abs(n - m)

    if d > 1:
        return 0
    elif d == 1:
        return fact(n) * fact(m) % M
    else:
        return fact(n) * fact(m) * 2 % M

def fact(n):
    s = 1
    for i in range(2, n + 1):
        s = (s * i) % M
    return s

main()

Solution (Python):
def factorial(n):
  ans = 1
  for i in range(n):
    ans *= i+1
    ans %= mod
  return ans

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

Solution (Python):
import math

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

Solution (Python):

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

Solution (Python):
from math import factorial as f

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

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

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

def a(x):
    ans=1
    for i in range(1,x+1):
        ans*=i
        ans%=mod
    return ans

if n==m:
    print(2*(a(n)**2)%mod)
elif n==m+1:
    print(a(n)*a(n-1)%mod)
elif m==n+1:
    print(a(m)*a(m-1)%mod)
else:
    print(0)

Solution (Python):
MOD = 10**9 + 7
def Mod_Permutation(n, k, mod):
    ret = 1
    for i in range(n - k + 1, n + 1):
        ret = ret * i % mod
    return ret
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
elif N == M:
    print(int(Mod_Permutation(N, N, MOD) * (2 * Mod_Permutation(N, M, MOD) % MOD) % MOD))
else:
    if N < M:
        T = N
        N = M
        M = T
    print(int(Mod_Permutation(N, N, MOD) * Mod_Permutation(M, M, MOD) % MOD))

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

MOD = 10 ** 9 + 7

if abs(n - m) > 1:
    print(0)
elif abs(n - m) == 1:
    if n < m:
        n, m = m, n
    temp = 1
    for i in range(1, n + 1):
        b = temp
        temp = temp * i % MOD
    print((temp * b) % MOD)
else:
    temp = 1
    for i in range(1, n + 1):
        temp = (temp * i) % MOD
    print((temp * temp * 2) % MOD)

Solution (Python):
a, b = map(int, input().split())

def A(n):
    ans = 1
    while(n != 1):
        ans = ans * n % 1000000007
        n -= 1
    return ans

if(a == b):
    t = A(a)
    print(t * t * 2 % 1000000007)
elif(a == b + 1 or b == a + 1):
    print(A(a) *A(b) % 1000000007)
else:
    print(0)

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

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

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

if abs(N - M) >1:
    print(0)
elif (N + M) % 2 == 0:
    print(x * 2 % (10 ** 9 + 7))
else:
    print(x % (10 ** 9 + 7))

Solution (Python):
mod = 10**9 + 7
a, b  = map(int,input().split())
k = abs(a-b)
def f(i):
  dest = 1
  while i != 0:
    dest *= i
    dest %= mod
    i -= 1
  return dest
if k >= 2:
  print(0)
elif k == 1:
  print( f(a) * f(b) % mod)
elif k == 0:
  print( 2 * f(a) * f(b) % mod)

Solution (Python):
n, m = map(int, input().split())
if abs(n-m) >= 2:
    print(0)
    exit()
res = 1
if n == m:
    for i in range(1, n+1):
        res *= i * i
        res %= 10**9 + 7
    res *= 2
else:
    for i in range(1, min(n, m)+1):
        res *= i * i
        res %= 10**9 + 7
    res *= max(n, m)
res %= 10**9 + 7
print(res)

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

def fact(i):
    count = 1
    for p in range(2,i+1):
        count *= p
        count = count % (10**9+7)
    return count
if N+1 < M or N > M+1:
    print(0)
else:
    if (N+M) % 2 == 0:
        print(fact(N)*fact(M)*2 % (10**9 + 7))
    else:
        print(fact(N)*fact(M)%(10**9 + 7))

Solution (Python):
import math

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

if N in [M, M+1, M-1]:
    n = math.factorial(N) * math.factorial(M)
    if N == M:
        print(n*2 % (10**9+7))
    else:
        print(n % (10**9+7))
else:
    print(0)

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

def factorical(n):
  ret = 1
  for i in range(1,n+1):
    ret *= i
    ret %= 10**9+7
  return ret

diff = abs(N - M)
max_ = int(1e9+7)

if(diff >= 2):
  print(0)
elif(diff == 1):
  n = N % max_
  m = M % max_
  print((factorical(n)*factorical(m)) % max_)
else:
  n = N % max_
  m = M % max_
  print((2*factorical(n)*factorical(m)) % max_)

Solution (Python):
import math

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

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

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

Solution (Python):



















from collections import defaultdict, Counter
import math
from bisect import bisect_left, bisect_right
import numpy as np

mod = 1000000007

def mul(a, b):
    return ((a % mod) * (b % mod)) % mod


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

if n==m:
    a = math.factorial(n)
    print(mul(mul(a, a), 2))
elif n==m-1 or n-1==m:
    if n<m:
        a = math.factorial(n)
        b = a*m
    else:
        a = math.factorial(m)
        b = a*n
    print(mul(a, b))
else:
    print(0)

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

def factorial_mod(A):
    kaiji = 1
    for i in range(A):
        kaiji*=(i+1)
        kaiji = kaiji%1000000007
    return kaiji
    
if sa >= 2:
    print(0)
elif sa == 1:
    n = N%1000000007
    m = M%1000000007
    nn = factorial_mod(n)
    mm = factorial_mod(m)
    ans =(nn*mm)%1000000007
    print(ans)
else:
    n = N%1000000007
    m = M%1000000007
    nn = factorial_mod(n)
    mm = factorial_mod(m)
    ans = (2*nn*mm)%1000000007
    print(ans)

Solution (Python):
from math import factorial
n,m=map(int,input().split())
mod= 10**9+7

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

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

ans = 1
if n == m:
  for i in range(1,n+1):
    ans = (ans*i)%mod
  for i in range(1,m+1):
    ans = (ans*i)%mod
  ans = (2*ans)%mod
    
elif n-1 == m or m-1 == n:
  for i in range(1,n+1):
    ans = (ans*i)%mod
  for i in range(1,m+1):
    ans = (ans*i)%mod
    
else:
  ans = 0
print(ans)

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

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

ans = 1
modulo = 10**9+7
if N==M:
    for i in range(N):
        ans *= (i+1)
        ans %= modulo
        ans *= (i+1)
        ans %= modulo
    ans *= 2
    ans %= modulo
else:
    A,B = max(M,N),min(N,M)
    if A-B==1:
        for i in range(N):
            ans *= (i+1)
            ans %= modulo
        for i in range(M):
            ans *= (i+1)
            ans %= modulo
    else:
        ans = 0

print(ans)

Solution (Python):
def solve(N, M):
    d = 10 ** 9 + 7
    N, M = min(N, M), max(N, M)
    if abs(N - M) >= 2:
        return 0
    elif abs(N - M) == 1:
        ans = M
    else:
        ans = 2
    for i in range(1, N + 1):
        ans *= pow(i, 2, d)
        ans %= d
    return ans


if __name__ == "__main__":
    N, M = tuple(map(int, input().split(" ")))
    print(solve(N, M))

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

Solution (Python):
def solve():
    [N, M] = [int(x) for x in input().split()]

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

    if abs(N - M) > 1:
        return 0

    Nfact = 1
    MOD = 10**9 + 7

    for i in range(1, N+1):
        Nfact = Nfact * i % MOD

    Nfact2 = Nfact * Nfact % MOD

    if N == M:
        return Nfact2 * 2 % MOD
    else:
        return (N+1) * Nfact2 % MOD


def main():
    print(solve())


if __name__ == '__main__':
    main()

Solution (Python):
def factorial_mod(n, mod=10**9+7):
    ret = 1
    for i in range(1, n+1):
        ret = (ret * i) % mod
    return ret

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

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

mod = 10**9 + 7
def kaijou(N):
	x = 1
	for i in range(N):
		x*=i+1
		x%=mod
	return x

kn = kaijou(N)
km = kaijou(M)
if abs(N-M) >=2:
	ans = 0

else:
	if abs(N-M) == 0:
		
		ans = kn*kn*2 % mod

	if abs(N-M) == 1:

		ans = kn * km % mod

print(ans)

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

fn = math.factorial(n)
fm = math.factorial(m)

if abs(n-m) >= 2:
    print(0)
else:
    if (n+m)%2 == 0:
        print(fn*fm*2%mod)
    else:
        print(fn*fm%mod)

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

g = [1, 1]
for i in range(2, M+1):
    g.append((g[-1] * i) % mod)

if M-N > 1:
    print(0)
elif M-N == 1:
    print(g[M]*g[N] % mod)
else:
    print(g[M]**2*2 % mod)

Solution (Python):
from math import factorial as F
N, M = map(int, input().split())
N, M = min(N, M), max(N, M)
mod = 10**9+7

if M-N > 1:
    print(0)
elif M-N == 1:
    print(F(M)*F(N) % mod)
else:
    print(F(M)**2*2 % mod)

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

g1 = [1, 1]
for i in range(2, M+1):
    g1.append((g1[-1] * i) % mod)

if M-N > 1:
    print(0)
elif M-N == 1:
    print(g1[M]*g1[N] % mod)
else:
    print(g1[M]**2*2 % mod)

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

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

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

def main(n, m):
    if n == m:
        return 2 * f(n) * f(m) % MOD
    if abs(n - m) == 1:
        return f(n) * f(m) % MOD
    return 0

if __name__ == '__main__':
    n, m = map(int, input().split())
    print(main(n, m))

Solution (Python):
import math
def a():return input()
def ai():return int(input())
def ma():return map(int, input().split())
def ms():return map(str, input().split())
def lma():return list(map(int, input().split()))
def lms():return list(map(str, input().split()))
k= []
a,b = ma()
mod = 10**9 + 7
m = max(a,b)
n = min(a,b)
k=math.factorial(n)
if abs(a-b)>1:print(0)
elif m!=n:
    print(((k**2)*m)%mod)
else:
    print(((k**2)*2)%mod)

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

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

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

if abs(N - M) == 0:
    ans = 2 * func(N)*func(M)
elif abs(N - M) == 1:
    ans = func(N)*func(M)
    
print(ans % MOD)

Solution (Python):
import math
n = sorted(list(int(i) for i in input().split()),reverse=True)
mod = 10**9+7
if n[0]-n[1] >= 2:
 
 ans = 0
elif n[0]-n[1] ==1:
 ans = ((math.factorial(n[0])%mod)*(math.factorial(n[1])%mod)%mod)
else:
 ans = (2*(math.factorial(n[0])%mod)*(math.factorial(n[1])%mod)%mod)    
print(ans)

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

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

def fact(n):
    ans = 1
    while n > 0:
        ans = (ans*n) % mod
        n -= 1
    return ans

N,M = map(int,input().split())
if N-M < -1 or N-M > 1:
    print(0)
elif N-M == 0:
    tmp = fact(N)
    print(2*tmp*tmp % mod)
else:
    print(fact(N)*fact(M) % mod)

Solution (Python):
from sys import stderr, setrecursionlimit, exit
from math import factorial
setrecursionlimit(2147483647)
def getInt():
    return int(input())
def getInts():
    return [int(i) for i in input().split()]
def getIntLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInt())
    return res
def getIntsLines(n=1):
    res = []
    for _ in range(n):
        res.append(getInts())
    return res
def debug(*args, **kwargs):
    print(*args, file=stderr, **kwargs)

mod = 10**9 + 7

n, m = getInts()
if abs(n-m) >= 2:
    print(0)
    exit()
else:
    a = factorial(min(n, m))
    if n == m:
        print((((a**2) % mod) * 2 ) % mod)
    else:
        print((((a**2) % mod) * max(m, n)) % mod)

Solution (Python):
n,m=map(int,input().split())
s=abs(n-m)
if s<=1:
  a=2-s
  for i in [n,m]:
    for j in range(1,i+1):
      a=a*j%(10**9+7)
else:
  a=0
print(a)

Solution (Python):

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

Solution (Python):
import math

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

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

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

def fac(n):
    ans = 1
    for i in range(1, n+1):
        ans *= i
        ans %= (10**9+7)
    return ans

if abs(N-M) >= 2:
    ans = 0
else:
    if N == M:
        ans = fac(N) * fac(M) * 2 % (10**9+7)
    else:
        ans = fac(N) * fac(M) % (10**9 + 7)

print(ans)

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


abc065_c()

Solution (Python):
def fact_10_9_7(x):
    ans = 1
    for i in range(1, x+1):
        ans = (i * ans) % (int(1e9) + 7)
    return ans

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

ans = 0
if N == M:
    ans = (fact_10_9_7(N) * fact_10_9_7(M) % (int(1e9) + 7)) * 2 % (int(1e9) + 7)
elif N == M+1 or N+1 == M:
    ans = fact_10_9_7(N) * fact_10_9_7(M) % (int(1e9) + 7)
print(ans)

Solution (Python):
MOD = 1000000007


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

    res = 2 if abs(n - m) == 0 else 1
    for i in range(2, n + 1):
        res = (res * i) % MOD
    for i in range(2, m + 1):
        res = (res * i) % MOD
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,input().split())
x=1
p=10**9+7
if abs(n-m)>1:print(0)
elif n==m:
  for i in range(1,n+1):
    x=(x*i)%p
  print((2*(x**2))%p)
else:
  for i in range(1,min(n,m)+1):
    x=(x*i)%p
  y=(x*max(n,m))%p
  print((x*y)%p)