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
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
class ModInt:
    def __init__(self, x):
        self.x = x % mod
    
    def __str__(self):
        return str(self.x)
    
    __repr__ = __str__

    def __add__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x + other.x)
        else:
            return ModInt(self.x + other)

    __radd__ = __add__
        
    def __sub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x - other.x)
        else:
            return ModInt(self.x - other)

    def __rsub__(self, other):
        if isinstance(other, ModInt):
            return ModInt(other.x - self.x)
        else:
            return ModInt(other - self.x)

    def __mul__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * other.x)
        else:
            return ModInt(self.x * other)

    __rmul__ = __mul__

    def __truediv__(self, other):
        if isinstance(other, ModInt):
            return ModInt(self.x * pow(other.x, mod-2,mod))
        else:
            return ModInt(self.x * pow(other, mod - 2, mod))
            
    def __rtruediv(self, other):
        if isinstance(other, self):
            return ModInt(other * pow(self.x, mod - 2, mod))
        else:
            return ModInt(other.x * pow(self.x, mod - 2, mod))
            

    def __pow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(self.x, other.x, mod))
        else:
            return ModInt(pow(self.x, other, mod))
            

    def __rpow__(self, other):
        if isinstance(other, ModInt):
            return ModInt(pow(other.x, self.x, mod))
        else:
            return ModInt(pow(other, self.x, mod))
def main():
    N,M=MI()
    if abs(N-M)>1:
        print(0)
    else:
        ans = ModInt(1)
        for i in range(1,N+1):
            ans *= i
        for j in range(1,M+1):
            ans *= j
        if abs(N-M)==0:
            ans*=2
        print(ans)
if __name__ == '__main__':
    main()

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

Solution (Python):
import numpy as np
import scipy as sp
import math

def main():
    
    N, M = parse(get_stdin())
    
    result = compute(N, M)
    
    print(result)

def compute(N, M):
    a = abs(N - M)
    if a >= 2:
        return 0
    elif a == 1:
        return (math.factorial(N) * math.factorial(M)) % (10**9+7)
    else:
        return 2*(math.factorial(N) * math.factorial(M)) % (10**9+7)

def get_stdin():
    return input()

def split_stdin(stdin_val, delimiter = ' '):
    return [x for x in stdin_val.split(delimiter) if x != '']

def parse(stdin_vars):
    vars = split_stdin(stdin_vars)
    return int(vars[0]), int(vars[1])

if __name__ == "__main__":
    main()

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

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

Solution (Python):
from math import factorial

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

ans = factorial(n) * factorial(m)
d = abs(n-m)  
if d == 0 :
  ans = 2*ans
elif d == 1:  
  pass
else:
  ans = 0

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

Solution (Python):
import math
import sys
sys.setrecursionlimit(10**6)
n, m = map(int, input().split())
mod = 10**9 + 7
def mod_factorial(n):
    ans = 1
    while n >= 2:
        ans  = (ans * n) % mod
        n -= 1
    return ans
if abs(n - m) == 1:
    print((mod_factorial(n) * mod_factorial(m)) % mod)
elif abs(n - m) == 0:
    print((mod_factorial(n) * mod_factorial(m) * 2) % mod)
else:
    print(0)

Solution (Python):
from sys import stdin
from collections import deque
from math import factorial
mod = 10**9 + 7
a,b = [int(x) for x in stdin.readline().rstrip().split()]

if abs(a-b) >= 2:
    print(0)
    exit()

if a == b:
    print(2*(factorial(a)%mod)**2%mod)
else:
    mi = min(a,b)
    print(factorial(mi+1)%mod*(factorial(mi)%mod)%mod)

Solution (Python):
def fact(n, div):
    ans = 1
    for i in range(2,n+1):
        ans *= i
        ans %= div
    return ans

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(1000000)
n,m = map(int,input().split())
p = 10**9 + 7

def fact(n):
  if n == 1:
    return 1
  return n*fact(n-1)%p

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

Solution (Python):
import math

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

diff = abs(a - b)
if diff >= 2:
  print(0)
  exit(0)
  
n = (10 ** 9) + 7
f = math.factorial(a) * math.factorial(b)
if diff == 1:
  print(f % n)
  exit(0)
  
print((2 * f) % n)

Solution (Python):
a,b = map(int,input().split())
if abs(a-b)>=2:
    print(0)
elif a!=b:
    mod = 10**9+7
    res = 1
    for i in range(a):
        res *=(i+1)
        res%=mod
    for i in range(b):
        res*=(i+1)
        res%=mod
    print(res)
else:
    res = 1
    mod = 10**9+7
    for i in range(a):
        res*=(i+1)
        res%=mod
    print(((res**2)*2)%mod)

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

Solution (Python):
from math import factorial

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

Solution (Python):
N, M = sorted([int(a) for a in input().split()])
fa = 1
P = 10 ** 9 + 7
for i in range(1, N+1):
    fa = fa * i % P

if N == M:
    print(fa*fa*2%P)
elif M - N == 1:
    print(fa*fa*M%P)
else:
    print(0)

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

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

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, M = MAP()

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

if N == M:
  print(((factorial[N]*factorial[M])%mod*2)%mod)
elif abs(N-M) == 1:
  print((factorial[N]*factorial[M])%mod)
else:
  print("0")

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

Solution (Python):
import math

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

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

if abs(N - M) > 1:
  ans = 0
elif N == M:
  ans = 2
  for i in range(1, N + 1):
    ans = (ans * i * i) % MOD
else:
  A = max(N, M)
  ans = 1
  for i in range(1, A):
    ans = (ans * i * i) % MOD
  ans = ans * A
print(int(ans % 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) % (10**9+7))
else:
    print(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)
    exit()

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

Solution (Python):
def examC(mod):
    N, M = LI()
    fac = [1]*max(N,M)
    for i in range(1,max(N,M)):
        fac[i] = (i+1)*fac[i-1]%mod
    if abs(N-M)==1:
        ans = fac[N-1]*fac[M-1]%mod
    elif abs(N-M)==0:
        ans = fac[N-1]*fac[M-1]*2%mod
    else:
        ans = 0
    print(ans)

import sys
import copy
import bisect
import heapq
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC(mod)

Solution (Python):
from sys import stdin,setrecursionlimit
import sys
from math import factorial
setrecursionlimit(10**6)
input=stdin.readline

a,b=map(int,input().split())
if abs(a-b)>1:
    print(0)
else:
    mod=10**9+7
    fact=[0]*(max(a,b)+1)
    fact[0],fact[1]=1,1
    for i in range(2,max(a,b)+1):
        fact[i]=(i*fact[i-1])%mod
    ans=(fact[a]*fact[b])%mod
    print(ans if abs(a-b)==1 else (ans*2)%mod)

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

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

if N > M:
    N, M = M, N
tmp = 1

MOD = 10 ** 9 + 7
for i in range(1, N + 1):
    tmp = (tmp * i) % MOD

if N == M:
    ans = ((tmp ** 2) * 2) % MOD
else:
    ans = (tmp * (tmp * M)) % MOD
print(ans)

Solution (Python):
import math
n, m =map(int, input().split())
num = 0
if abs(n-m) >= 2:
	print (0)
	exit()
if n == m:
	num = math.factorial(n) * math.factorial(n) * 2
else:
	num = math.factorial(n) * math.factorial(m)
print (num % 1000000007)

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

if abs(n - m) > 1:
    res = 0
else:
    a = 1
    for x in range(1, n+1):
        a = (a * x) % mod
    b = 1
    for x in range(1, m+1):
        b = (b * x) % mod
    res = (a * b) % mod
    if n == m:
        res = (res * 2) % mod

print(res)

Solution (Python):
import math

animals = list(map(int, input().split(' ')))
dog = max(animals)
monkey = min(animals)
ans = 0



if dog - monkey == 0:
    ans = 2 * (math.factorial(dog) * math.factorial(monkey))
elif dog - monkey == 1:
    ans = math.factorial(dog) * math.factorial(monkey)
else:
    ans = 0
    
print(ans % (10 ** 9 + 7))

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

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

Solution (Python):
N,M=map(int,input().split())
mod=10**9+7
if N>M+1 or M>N+1:
    print(0)
    exit()
value=1
if N==M:value*=2
for n in range(1,N+1):
    value*=n
    if value>mod:
        value%=mod
for m in range(1,M+1):
    value*=m
    if value>mod:
        value%=mod
print(value)

Solution (Python):
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
        ans%=10**9+7
    for i in range(1,M+1):
        ans=ans*i
        ans%=10**9+7
    if N==M:
        print(2*ans%(10**9+7))
    else:
        print(ans)

Solution (Python):
def main():
    MOD = 10**9+7
    N, M = (int(i) for i in input().split())
    if abs(N - M) > 1:
        print(0)
    elif N == M:
        from math import factorial
        print(factorial(N) * factorial(M) * 2 % MOD)
    else:
        from math import factorial
        print(factorial(N) * factorial(M) % MOD)


if __name__ == '__main__':
    main()

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

Solution (Python):
n,m=map(int,input().split())
mod=10**9+7
if abs(n-m)>=2:
    print(0)
elif abs(n-m)==1:
    ans=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)
else:
    ans=2
    for i in range(1,n+1):
        ans=(ans*i)%mod
    for i in range(1,m+1):
        ans=(ans*i)%mod
    print(ans)

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

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



if __name__ == "__main__":
    main()

Solution (Python):
from math import factorial

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

Solution (Python):
import math

N,M = map(int,input().split())
ans = 1
if N-M >= 2 or M-N >= 2:
    print(0)
else:
    if N > M:
        for i in range(1,N+1):
            if i == N:
                ans *= i
            else:
                ans *= i**2
            ans = ans % 1000000007
    elif N < M:
        for i in range(1,M+1):
            if i == M:
                ans *= i
            else:
                ans *= i**2
            ans = ans % 1000000007
    elif N == M:
         for i in range(1,N+1):
                ans *= i**2
                if i == N:
                    ans *= 2
                ans = ans % 1000000007
    print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
from math import factorial 
mod = 10 ** 9 + 7

N, M = [int(x) for x in input().split()]
if abs(N - M) >= 2: ans = 0
else: ans = (factorial(N) % mod * factorial(M) % mod) % mod
if N == M: ans *= 2
print(ans % mod)

Solution (Python):
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    SSSS = 10 ** 9 + 7
    [A, B] = string_to_int(inputs[0])
    max_len = max([A, B])
    min_len = min([A, B])
    diff_len = abs(A-B)
    if diff_len >= 2:
        return 0

    pattern = 0
    for i in reversed(range(1, min_len + 1)):
        if i == min_len:
            if diff_len == 1:
                pattern = max_len * min_len
                pattern *= min_len
            else:
                pattern = min_len + min_len
                pattern *= min_len
        else:
            pattern *= i * i
        pattern = pattern % SSSS

    return pattern % SSSS


def string_to_int(string):
    return list(map(int, string.split()))


if __name__ == "__main__":
    ret = solve(inputs(1))
    print(ret)

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
 
mod = 10**9+7
 
N,M = map(int,input().split())
 
facterint = [1]
for i in range(1,max(N,M)+1):
    facterint.append(facterint[-1]*i%mod)
 
 
if abs(N-M) > 1:
    print(0)
    exit()
 
if N==M:
    ans = 2*facterint[N]*facterint[M]%mod
else:
    ans = facterint[N]*facterint[M]%mod
 
print(ans)

Solution (Python):
N,M=map(int,input().split())
MOD=10**9+7
import math
Nb=math.factorial(N)%MOD
Mb=math.factorial(M)%MOD

if abs(N-M)>=2:
    print(0)
elif abs(N-M)==1:
    print(Nb*Mb%MOD)
else:
    print(Nb*Mb*2%MOD)

Solution (Python):
import math
import sys


N, M = map(int, input().split())
C = 10**9 + 7

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

if mm != nn and mm + 1 < nn:
    print(0)
    sys.exit()

if N == M:
    pN = math.factorial(N) % C
    pM = math.factorial(M) % C
    p = pN * pM * (2 % C)
    print(p % C)

else:
    pN = math.factorial(N) % C
    pM = math.factorial(M) % C
    print((pN * pM) % C)

Solution (Python):

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

devided_number = 7 + 10**9

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

Solution (Python):





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

def gcd(a, b):
    if b == 0:
        return a
    else:
        return gcd(b, a%b)
def lcm(a, b):
    return((a*b)//gcd(a,b))
def div(x): 
    div=[1]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        if(x%check==0):
            div.append(check)
            while(x%check==0):
                x=x//check
        check+=1
    if x != 1:
      div.append(x)
    return div
def div2(x): 
    div2=[]
    check=2
    while(x!=1 and check <= int(x**0.5)+2):
        while x%check==0:
            div2.append(check)
            x/=check
        check+=1
    if x != 1:
      div2.append(x)
    return div2

def main():
    n,m = map(int,input().split())    
    if abs(n-m)>1:
        print(0)
    else:
        a=max(n,m)
        b=min(n,m)
        if a>b:
            print((factorial(a)*factorial(b))%mod)
        else:
            ans=(factorial(a)**2)%mod
            print((2*ans)%mod)
main()

Solution (Python):
import sys

MOD = (10**9)+7
N,M = map(int,input().split())
if not(N == M or N+1 == M or N == M+1):
    print(0)
    sys.exit()

if N == M:
    ans = 2
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
print(ans)

Solution (Python):
import math 

INF = 10**9 + 7
n,m = map(int, input().split())
ans = 0
n,m = max(n,m),min(m,n)
a = math.factorial(n)%INF
b = math.factorial(m)%INF
ans = a*b
if n==m:
    ans = ans*2
    ans = ans%INF
elif n ==m+1:
    ans = ans%INF
else:
    ans = 0
print(ans)

Solution (Python):

import sys
import math
MOD = 1000000007  


def solve(N: int, M: int):
    if abs(N-M) >= 2:
        print(0)
        return 
    
    
    N_number = math.factorial(N)%MOD
    M_number = math.factorial(M)%MOD

    if N == M:
        print((N_number*M_number*2)%MOD)
    else:
        print((N_number*M_number)%MOD)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    M = int(next(tokens))  
    solve(N, M)

if __name__ == '__main__':
    main()

Solution (Python):

def fact(x):
    if x<1:
        return 1
    global p
    res=1
    for i in range(2, x+1):
        res=(res*i)%p
    return res

p=10**9+7
N,M=map(int,input().split())

if abs(N-M)>1:
    ans=0
else:
    ans=(fact(N)*fact(M))%p
if N==M:
    ans=(ans*2)%p

print(ans)

Solution (Python):

def inv(x):
    global p
    res=1
    prod=x
    e=p-2
    while e>0:
        if e%2==1:
            res=(res*prod)%p
        prod=(prod*prod)%p
        e//=2
    return res


def fact(x):
    if x<1:
        return 1
    global p
    res=1
    for i in range(2, x+1):
        res=(res*i)%p
    return res

p=10**9+7
N,M=map(int,input().split())

if abs(N-M)>1:
    ans=0
else:
    ans=(fact(N)*fact(M))%p
if N==M:
    ans=(ans*2)%p

print(ans)

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

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

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

Solution (Python):
import math
a,b = [int(i) for i in input().split()]
mod = 10**9 +7

if a+1 < b:
    print(0)
elif a > b+1:
    print(0)
elif a == b:
    print(math.factorial(a)%mod*math.factorial(b)%mod*2%mod)
else:
    print(math.factorial(a)%mod*math.factorial(b)%mod)

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

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

Solution (Python):
import sys
input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline


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


if __name__ == '__main__':
    main()

Solution (Python):
import math
N, M = map(int,input().split())
N, M = max(N,M), min(N,M)
mod = 10**9 + 7
if N-M != 1 and N != M:
    ans = 0
elif N != M:
    factN = math.factorial(N)
    ans = (factN*factN//N)%mod
else:
    factN = math.factorial(N)
    ans = (factN*factN*2)%mod
print(ans)

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

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


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = i * FCT % INF
    return FCT


def main():
    N, M = map(int, input().split())
    if N == M:
        print(2 * factorial(min(N, M)) ** 2 % INF)
    elif abs(N - M) <= 1:
        print(factorial(min(N, M)) ** 2 * max(N, M) % INF)
    else:
        print(0)

    return


main()

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


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = i * FCT % INF
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M:
        print(2 * MIN_NM ** 2 % INF)
    elif abs(N - M) <= 1:
        print(MIN_NM ** 2 * max(N, M) % INF)
    else:
        print(0)

    return


main()

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


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = i * FCT % INF
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M:
        print(2 * MIN_NM * MIN_NM % INF)
    elif abs(N - M) <= 1:
        print(MIN_NM * MIN_NM * max(N, M) % INF)
    else:
        print(0)

    return


main()

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


def modmulti(a, b):
    
    return a * b % INF


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = modmulti(i, FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM)))
    elif abs(N - M) <= 1:
        print(modmulti(MIN_NM, modmulti(MIN_NM, max(N, M))))
    else:
        print(0)

    return


main()

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


def modmulti(a, b):
    
    return a * b % INF


def factorial(x):
    FCT = 1
    for i in range(2, x + 1):
        FCT = modmulti(i, FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM))
              if N == M else modmulti(MIN_NM, modmulti(MIN_NM, max(N, M))))
    else:
        print(0)

    return


main()

Solution (Python):
from functools import reduce
INF = int(1e9 + 7)


def modmulti(a, b):
    
    return a * b % INF


def factorial(x):
    FCT = 1
    FCT = reduce(modmulti, range(2, x + 1), FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    MIN_NM = factorial(min(N, M))
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(MIN_NM, MIN_NM))
              if N == M else modmulti(MIN_NM, modmulti(MIN_NM, max(N, M))))
    else:
        print(0)

    return


main()

Solution (Python):
from functools import reduce
INF = int(1e9 + 7)


def modmulti(a, b):
    
    return a * b % INF


def factorial(x):
    FCT = 1
    FCT = reduce(modmulti, range(2, x + 1), FCT)
    return FCT


def main():
    N, M = map(int, input().split())
    if N == M or N + 1 == M or N == M + 1:
        print(modmulti(2, modmulti(factorial(N), factorial(M)))
              if N == M else modmulti(factorial(N), factorial(M)))
    else:
        print(0)

    return


main()

Solution (Python):
INF = int(1e9 + 7)
FCT = [0] * (int(1e5) + 1)


def modmulti(a, b):
    
    return a * b % INF


def factorial(x):
    FCT[0] = 1
    FCT[1] = 1
    for i in range(2, x + 1):
        FCT[i] = modmulti(FCT[i - 1], i)


def main():
    N, M = map(int, input().split())
    if N == M or N + 1 == M or N == M + 1:
        if N < M:
            factorial(M)
        else:
            factorial(N)
        print(modmulti(2, modmulti(FCT[N], FCT[M]))
              if N == M else modmulti(FCT[N], FCT[M]))
    else:
        print(0)

    return


main()

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

Solution (Python):

N,M = map(int,input().split())
mod = 10**9+7
ans = 0
dog = 1
monkey = 1
if abs(N-M) > 1:
    print(ans)
    exit()
elif N == M:
    for i in range(1,N+1):
        dog = (dog*i)%mod

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

elif abs(N-M) <= 1:
    for i in range(1,N+1):
        dog = (dog*i)%mod

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

print(ans)

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

Solution (Python):
import math

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

Solution (Python):
n,m=map(int,input().split())
def f(n):
    c=1
    for i in range(1,n+1):
        c*=i
        if c>=(10**9+7):
            c=c%(10**9+7)
    return c
if abs(n-m)==1:
    s=f(n)*f(m)%(10**9+7)
    print(s)
elif n==m:
    s=f(n)*f(m)*2%(10**9+7)
    print(s)
else:
    print(0)