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):
from math import factorial as f
n,m = map(int, input().split())
print(max(0,2-abs(n-m))*f(n)*f(m) % (10**9+7))

Solution (Python):
import itertools

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

n_p = 1
m_p = 1

for i in range(1,n+1):
    n_p *= i
    n_p %= (10**9+7)

for i in range(1,m+1):
    m_p *= i
    m_p %= (10**9+7)


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

elif abs(n-m) == 1:
    print((n_p*m_p)%(10**9+7))

else:
    print((2*n_p*m_p)%(10**9+7))

Solution (Python):
import math

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

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

else:
	ans = math.factorial(N) * math.factorial(M)

	if(N==M):
		ans *= 2
	ans %= (10 ** 9 +7)

	print(ans)

Solution (Python):
import math
a=list(map(int,input().split()))
ans=0
if max(a)-min(a)>=2:
    print(0)
else:
    if a[0]==a[1]:
        ans=math.factorial(max(a))
        ans=(ans%1000000007)*(ans%1000000007)*2
    else:
        ans=math.factorial(max(a))
        ans1 = math.factorial(min(a))
        ans=(ans%1000000007)*((ans1)%1000000007)
    print(int(ans%1000000007 ))

Solution (Python):
import math

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

mod = int(1e9+7)

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

print(ans)

Solution (Python):
import math

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

mod = int(1e9+7)

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

print(ans)

Solution (Python):
import math

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

mod = int(1e9+7)

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

print(ans)

Solution (Python):
import math

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

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

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


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

for i in range(1,N+1):
    tmp = (tmp * i) % 1000000007

for i in range(1, M+1):
    tmp = (tmp * i) % 1000000007

print(tmp)

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

ans = 0

if abs(N-M) ==0:
  ans = math.factorial(N) * math.factorial(M) * 2
elif abs(N-M) == 1:
  ans = math.factorial(N) * math.factorial(M)
else:
  ans = 0

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

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

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

Solution (Python):
import sys
import math

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

n = math.factorial(N)
m = math.factorial(M)
MOD = 10**9 + 7

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

Solution (Python):
def fct(n):
	v = 1
	for i in range(1, n+1):
		v = (v * i) % 1000000007
	return v
N, M = map(int, input().split())
ans = 0
if N == M:
	v = fct(N)
	ans = (v*v*2)%1000000007
elif abs(N-M) == 1:
	ans = (fct(N)*fct(M))%1000000007
print(ans)

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

inf = 10**9+7
def f(num,total):
	for n in range(1,num+1):
		total *= n
		total %= inf
	return total

if abs(n-m) > 1:
	ans = 0
elif abs(n-m) == 1:
	ans = f(n,1)*f(m,1)
	ans %= inf
else:
	ans = f(n,1)*f(m,1)*2
	ans %= inf
print(ans)

Solution (Python):
import math

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

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

print((2 if (N == M) else 1) * math.factorial(N) * math.factorial(M) % (10 ** 9 + 7))

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

Solution (Python):
from math import factorial

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

if abs(N - M) == 0:
	print(((factorial(N) ** 2) * 2) % INF)
else:
	print((factorial(N) % INF) * (factorial(M) % INF) % INF)

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

Solution (Python):
M, N = map(int, input().split())
def fraction(N):
  a = N
  for x in range(N-1,1,-1):
    a = a * x % (1000000007)
  return a

p = abs(M - N)
if p < 2:
  print(fraction(M) * fraction(N) * (2 - p) % (1000000007))
else:
  print(0)

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

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  _n,_m=LI()

  ans=1
  ans2=1
  if abs(_n-_m)>1:
    return 0
    exit()
  else:
    n=_n
    m=_m
    while True:
      if n==0 or m==0:
        break
      ans*=n
      n-=1
      ans%=mod
      ans*=m
      m-=1
      ans%=mod
    n=_n
    m=_m
    while True:
      if n==0 or m==0:
        break
      ans2*=n
      n-=1
      ans2%=mod
      ans2*=m
      m-=1
      ans2%=mod
    if abs(_n-_m)==1:
      return ans%mod
    return (ans+ans2)%mod

print(main())

Solution (Python):

a, b = [ int(v) for v in input().split() ]
mod = 10**9+7
ans = 0
if a == b:
	p = 1
	for i in range(a):
		p = ( (i+1) * p ) % mod
	ans = (p*p*2)%mod

elif abs(a - b) == 1:
	if a < b:
		a, b = b, a
	p = 1
	for i in range(b):
		p = ( (i+1) * p ) % mod
	ans = (p*p*a)%mod
print(ans)

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

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

def f(n):
    r = 1
    for i in range(1, n + 1):
        r = r * i % (10 ** 9 + 7)
        
    return r
    
if N == M:
    print(f(N) * f(M) * 2 % (10 ** 9 + 7))
elif abs(N - M) == 1:
    print(f(N) * f(M) % (10 ** 9 + 7))
else:
    print(0)

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

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

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)**2)) % (10**9+7) )
else:
    if N < M:   N,M = M,N
    print( ((math.factorial(M)**2)*N) % (10**9+7) )

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


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

if abs(n - m) > 1:
    print(0)
    exit()

x = factorial_mod(n, mod) * factorial_mod(m, mod)
print((x * 2 % mod) if n == m else x % mod)

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


def n_factorial_mod(n, mod):
    ret = 1
    while n != 1:
        ret = ret * n % mod
        n -= 1
    return ret


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

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

if N <= M:
    N, M = M, N

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

num = 10**9 + 7


def bikkuri(n):
    rt = 1
    for i in range(n, 0, -1):
        rt *= i
        rt = rt % num
    return rt


ans = (bikkuri(M)**2) % num

if N == M:
    ans *= 2
else:
    ans *= (M + 1)

ans %= num
print(ans)

Solution (Python):


import itertools
from collections import Counter
from collections import defaultdict
import bisect
import math
import heapq

def main():
    N, M = map(int, input().split())

    mod = 10**9 + 7

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

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

    if N == M:
        print((n * m * 2) % mod)
    else:
        print((n * m) % mod)

if __name__ == '__main__':
    main()

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

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

Solution (Python):


from math import factorial
MOD = 10**9 + 7

n,m = (int(num) for num in input().split())

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

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

Solution (Python):

from math import factorial

def solve():
    N, M = list(map(int, input().split()))
    MOD = 10**9+7
    """
    ありうるパターンは、
    
    ・NとMが同数
    ・NとMの差が1

    （例）
    ・2 2
    犬 猿 犬 猿
    この場合、犬と猿の位置を入れ替えたパターンもあるので×2

    ・2 3
    犬 猿 犬 猿 犬

    組み合わせの数は、順列の公式を使う
    
    ・順列の公式
    異なるn個のものからr個を取り出し並べる場合の数は
    nPr = n(n-1)(n-2)...(n-r+1) = n!/(n-r)!
    """

    def permutation_formula(n, r):
        return factorial(n) // factorial(n-r)

    if N == M:
        ans = (permutation_formula(N, N)%MOD) * (permutation_formula(M, M)%MOD) * 2
        print(ans%MOD)
        return
    elif abs(N-M) == 1:
        ans = (permutation_formula(N, N)%MOD) * (permutation_formula(M, M)%MOD)
        print(ans%MOD)
    else:
        print(0)
        return


if __name__ == "__main__":
    solve()

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

fac=[0]*(1+max(N,M))
fac[1]=1

for i in range(2, len(fac)):
	fac[i] = i * fac[i-1] % MOD

ans = fac[N] * fac[M] % MOD

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

print(ans)

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

Solution (Python):
a,b=list(map(int,input().strip().split()))
if a-b>1 or b-a>1:
	print(0)
else:
	ans=1
	if a==b:
		ans=ans*2
	for i in range(1,a+1,1):
		ans=ans*i%1000000007
	for i in range(1,b+1,1):
		ans=ans*i%1000000007
	print(ans)

Solution (Python):


import math



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


def main():
  NM = sorted(list(map(int, input().split())))
  n,m = [fact(NM[0]), fact(NM[1])]
  if NM[0]==NM[1]:
    return (n*m*2) % D
  elif NM[0]+1==NM[1]:
    return (n*m) % D
  else:
    return 0

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

Solution (Python):


import math



D = 10**9+7

def main():
  NM = sorted(list(map(int, input().split())))
  if NM[1]-NM[0] > 1:
    return 0
  elif NM[0]==NM[1]:
    ret = 2*(math.factorial(NM[0])**2) % D
    return ret
  else:
    ret = math.factorial(NM[0])*math.factorial(NM[1]) % D
    return ret

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

Solution (Python):
N,M = [int(x) for x in input().split()]
di = 10**9 + 7
if(abs(N-M)>1):
    ans = 0
else:
    ans = 1
    if(N!=M):
        ans *= max(N,M)
    else:
        ans *= 2
    for i in range(1,min(N,M)+1):
        ans = (ans * i**2) % di
print(ans)

Solution (Python):
from statistics import mean, median,variance,stdev
import numpy as np
import sys
import math
import fractions
import itertools
import copy

def j(q):
    if q==1: print("Yes")
    elif q == 0:print("No")
    exit(0)
rem = pow(10,9)+7
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def printrow(a):
    for i in range(len(a)):
        print(a[i])
def combinations(n,r):
    if n<r:return 0
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
def permutations(n,r):
    if n<r:return 0
    return math.factorial(n) // math.factorial(n - r)
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)



a,b= (int(i) for i in input().split())       








if abs(a-b)>1:
    print(0)
    exit(0)
else :
    s = 1
    for i in range(1, a + 1):
        s*=i
        s%=rem
    for i in range(1, b + 1):
        s *= i
        s %= rem
    if a == b: s*=2
    s%=rem
    print(s)

Solution (Python):
from math import factorial
 

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

Solution (Python):
from functools import lru_cache


@lru_cache(maxsize=None)
def fact(n):
    ret = 1
    MOD = 10**9 + 7
    for i in range(1, n + 1):
        ret = ret * i % MOD
    return ret


N, M = map(int, input().split())
MOD = 10**9 + 7
diff = abs(N - M)


if diff == 0:
    ans = (fact(N) * fact(M)) * 2
elif diff == 1:
    ans = fact(N) * fact(M)
else:
    ans = 0


ans %= MOD
print(ans)

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


N, M = map(int, input().split())
MOD = 10**9 + 7
diff = abs(N - M)


if diff == 0:
    ans = (fact(N) * fact(M)) * 2
elif diff == 1:
    ans = fact(N) * fact(M)
else:
    ans = 0


ans %= MOD
print(ans)

Solution (Python):
import sys
from math import factorial


sys.setrecursionlimit(500000)
N, M = map(int, input().split())
MOD = 10**9 + 7
diff = abs(N - M)


if diff == 0:
    ans = (factorial(N) * factorial(M)) * 2
elif diff == 1:
    ans = factorial(N) * factorial(M)
else:
    ans = 0


ans %= MOD
print(ans)

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

mod = 10 ** 9 + 7

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

if N == M:
    print(2 * mod_fact(N, mod) ** 2 % mod)
elif abs(N - M) == 1:
    print(mod_fact(min(M, N), mod) ** 2 * max(M, N) % mod)
else:
    print(0)

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

mod = 10 ** 9 + 7

def mod_fact(n, mod = 10 ** 9 + 7):
    result = 1
    for i in range(2, n + 1):
        result = (result * i) % mod
    return result

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

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

Solution (Python):
import math

N, M = map(int, input().split())
mod = int(1e9+7)

if N < M:
    N, M = M, N

if N == M:
    ans = (2 * (math.factorial(N)) ** 2) % mod

elif N == M + 1:
    ans = ((math.factorial(M)) ** 2 * N) % mod

else:
    ans = 0

print(ans)

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

for i in range(1, M+1) :
    monkey *= i
    monkey %= 10**9 + 7

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

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

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

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

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

mod = 10**9+7
d = [1]*100001
for i in range(1,100001):
    d[i] = d[i-1] * i % mod

if N == M:
    a = d[N]
    print(2 * a * a % mod)
elif N == M + 1:
    a = d[N]
    b = d[M]
    print(a * b % mod)
elif N + 1 == M:
    a = d[N]
    b = d[M]
    print(a * b % mod)
else:
    print(0)

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

Solution (Python):
from math import factorial
from sys import exit, stdin
N, M = [int(_) for _ in stdin.readline().rstrip().split()]
p_n = factorial(N)
p_m = factorial(M)
NUM = 10**9+7
if abs(N-M)==0:
    ans = 2*p_n*p_m
elif abs(N-M)==1:
    ans = p_n*p_m
else:
    ans = 0
print(ans%NUM)

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

MOD = 10 ** 9 + 7

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

print(ans)

Solution (Python):
n, m = map(int, input().split())
if abs(n - m) > 1:
    print(0)
    import sys
    sys.exit()


nn = 1
for i in range(1, n + 1):
    nn *= i
    nn %= 10**9+7
mm = 1
for i in range(1, m + 1):
    mm *= i
    mm %= 10**9+7
if n==m:
    print(2*nn*mm%(10**9+7))
else:
    print(nn*mm%(10**9+7))

Solution (Python):
from collections import Counter
from functools import reduce

import bisect
import copy
import fractions
import math
import pprint
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers):
    return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers):
    return reduce(LCM, numbers)
def LCM(m, n):
    return (m * n // fractions.gcd(m, n))
def unite(x, y):
    
    x = root(x)
    y = root(y)
    if x == y:
        return
    
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  
        return x
    else:
        node[x] = root(node[x])  
        return node[x]
def dfs(v):
    
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  
            continue
        if visited[v2] == True:  
            continue
        dfs(v2)


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

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

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

Solution (Python):

def read_input():
    n, m = map(int, input().split())

    return n, m

def submit():
    n, m = read_input()
    limit = 10**9 + 7

    if n == m:
        acc = 1

        for i in range(1, n + 1):
            acc *= i
            if acc > limit:
                acc %= limit

        for i in range(1, m + 1):
            acc *= i
            if acc > limit:
                acc %= limit

        print(2*acc%limit)

    else:
        big = max(n, m)
        small = min(n, m)

        if big - small > 1:
            print(0)
        else:
            acc = 1
            for i in range(1, big + 1):
                acc *= i
                if acc > limit:
                    acc %= limit

            for i in range(1, small + 1):
                acc *= i
                if acc > limit:
                    acc %= limit

            print(acc)





if __name__ == '__main__':
    submit()

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

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

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):
import math
n,m=map(int,input().split())
ans=math.factorial(m)*math.factorial(n)%(10**9+7)
if n==m:
  ans*=2
  ans%=(10**9+7)
elif abs(n-m)>1:
  ans=0
print(ans)

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


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


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

Solution (Python):
import math

divider = 1000000007

n, m = map(int, input().split())
diff = abs(n - m)

if diff == 0:
    print(2 * math.factorial(n) * math.factorial(m) % divider)
elif diff == 1:
    print(math.factorial(n) * math.factorial(m) % divider)
else:
    print(0)

Solution (Python):
a,b=map(int,input().split())
mod=1000000007
ans=1
if abs(a-b)>1:
  print(0)
else:
  if abs(a-b)==0:
    ans=2
  while a>1:
    ans*=a
    ans%=mod
    a-=1
  while b>1:
    ans*=b
    ans%=mod
    b-=1
  print(ans)

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

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

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

Solution (Python):
import math

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

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

Solution (Python):
from functools import reduce
import math

def main():
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    INF = float("inf")
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    N,M = (int(_) for _ in input().split())
    ans = 0
    if(abs(N-M)>1):
        print(0)
        exit(0)
    if(abs(N-M)==1):
        ans = math.factorial(N) * math.factorial(M) % (10**9+7)
    else:
        ans = (math.factorial(N) * math.factorial(M))*2 % (10**9+7)
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
n,m=map(int,input().split())
if abs(n-m)>1:ans=0
elif abs(n-m)==1:ans=1
else: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)

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

num_b = max([n,m]) 
num_l = min([n,m]) 
ans = 0
if num_b - num_l >= 2: 
  ans = 0
elif num_b - num_l == 1:
  
  
  ans = math.factorial(num_b) * math.factorial(num_l) % (10 ** 9 + 7)
else:
  
  
  ans = math.factorial(num_b) * num_l * math.factorial(num_l - 1) * 2 % (10 ** 9 + 7)
  
print(ans)

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

D = 10**9 + 7
if abs(n-m) == 0:
    res = 1
    for i in range(1,n+1):
        res = (res * i)%D
        res = (res * i)%D
    res = (res * 2)%D
    print(res)
elif abs(n-m) == 1:
    n,m = max(n,m),min(n,m)
    res = 1
    for i in range(1,n+1):
        res = (res * i)%D
    for i in range(1,m+1):
        res = (res * i)%D
    print(res)
else:
    print(0)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
mo=10**9+7
def fact(x):
  if x==0:
    return 1
  else:
    return x*fact(x-1)%mo
n,m=map(int,input().split())
if abs(n-m)>1:
  print(0)
elif m==n:
  print((2*fact(m)*fact(n))%mo)
else:
  print((fact(m)*fact(n))%mo)

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:
    ans = math.factorial(N) ** 2 * 2
elif N - M == 1:
    ans = math.factorial(M) ** 2 * N
else:
    ans = 0

print(ans % MOD)

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)
N,M=LI()
if abs(N-M) >= 2:
    print(0)
else:
    ans=math.factorial(N)*math.factorial(M)
    if abs(N-M) == 0:
        ans *= 2
    print(ans%mod)

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

Solution (Python):
import sys
import collections
sys.setrecursionlimit(100000)
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, m = nm()
memo = [1 for _ in range(max(n, m) + 1)]
mod = 10 ** 9 + 7

for i in range(1, max(n, m) + 1):
    memo[i] = memo[i - 1] * i % mod

if abs(n - m) >= 2:
    print(0)
else:
    nn = memo[n] % mod
    mm = memo[m] % mod
    if n == m:
        print(2 * nn * mm % mod)
    else:
        print(nn * mm % mod)

Solution (Python):
MOD = 10**9 + 7
n = sorted([int(item) for item in input().split()])

def nPr(n, r):
    val = 1
    for i in range(r):
        val = val * (n-i) % MOD 
    return val

if n[1] - n[0] >= 2:
    print(0)
    exit()

ans = nPr(n[0], n[0]) * nPr(n[1], n[1])
if n[1] == n[0]:
    ans *= 2
ans %= MOD

print(ans)

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

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

Solution (Python):

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

Solution (Python):
mod=10**9+7
N,M=map(int,input().split())
d=abs(N-M)
if d>1:
	print(0)
else:
	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*(2-d))%mod)

Solution (Python):
import math
n, m = map(int, input().split())
MOD = 1000000007
count_n = 0
count_m = 0
count = 0
if abs(n-m) == 1:
  count_n = math.factorial(n) % MOD
  count_m = math.factorial(m) % MOD
  count = count_m * count_n % MOD
elif abs(n-m) == 0:
  count_n = math.factorial(n) % MOD
  count_m = math.factorial(m) % MOD
  count = 2 * count_m * count_n % MOD
else:
  count = 0

print(count % MOD)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
n,m=map(int,input().split())
def fact(n):
    mod=10**9+7
    if n==0:
        return 1
    else:
        return (n*fact(n-1))%mod
mod=10**9+7
if n==m:
    print(2*fact(n)*fact(m)%mod)
elif n==m+1 or m==n+1:
        print(fact(n)*fact(m)%mod)
else:
    print(0)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
n,m=map(int,input().split())
def fact(n):
    mod=10**9+7
    if n==0:
        return 1
    else:
        return (n*fact(n-1))%mod
mod=10**9+7
if n==m:
    print(2*fact(n)*fact(m)%mod)
elif n==m+1 or m==n+1:
        print(fact(n)*fact(m)%mod)
else:
    print(0)

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

if(abs(N-M) > 1):
  ans = 0
elif(abs(N-M) == 0):
  a = math.factorial(N)
  ans = (2*a*a)%(10**9+7)
elif(abs(N-M) == 1):
  a = math.factorial(N)
  b = math.factorial(M)
  ans = (a*b)%(10**9+7)
print(ans)

Solution (Python):
x = list(map(int, input().split()))
MOD = 10 ** 9 + 7
if abs(x[0] - x[1]) > 1:
    print(0)
else:
    ans = 1
    for i in range(max(x)):
        for j in range(2):
            ans *= max(1, x[j] - i)
            ans = ans % MOD
    if x[0] == x[1]:
        print(ans * 2 % MOD)
    else:
        print(ans)

Solution (Python):
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))%(1000000007))
else:
    print((math.factorial(N)*math.factorial(M)*2)%(1000000007))

Solution (Python):
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))%(1000000007))
else:
    print((math.factorial(N)*math.factorial(M)*2)%(1000000007))