Problem:
 Score : 300 points    Problem Statement Snuke has N dogs and M monkeys. He wants them to line up in a row.  As a Japanese saying goes, these dogs and monkeys are on bad terms. ("ken'en no naka", literally "the relationship of dogs and monkeys", means a relationship of mutual hatred.) Snuke is trying to reconsile them, by arranging the animals so that there are neither two adjacent dogs nor two adjacent monkeys.  How many such arrangements there are? Find the count modulo 10^9+7 (since animals cannot understand numbers larger than that).
Here, dogs and monkeys are both distinguishable. Also, two arrangements that result from reversing each other are distinguished.      Constraints  1 ≤ N,M ≤ 10^5         Input Input is given from Standard Input in the following format:  N  M       Output Print the number of possible arrangements, modulo 10^9+7 .        Sample Input 1 2 2      Sample Output 1 8  We will denote the dogs by A and B , and the monkeys by C and D . There are eight possible arrangements: ACBD , ADBC , BCAD , BDAC , CADB , CBDA , DACB and DBCA .       Sample Input 2 3 2      Sample Output 2 12       Sample Input 3 1 8      Sample Output 3 0       Sample Input 4 100000 100000      Sample Output 4 530123477

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


def readInts():
  return list(map(int,input().split()))
mod = 10**9 + 7
def main():
  from math import factorial as f
  n,m = sorted(readInts())
  
  sa = m - n
  if sa >= 2:
    ans = 0
  else:
    ans = f(m) * f(n)
    if sa == 0:
      ans *= 2
  print(ans % mod)
if __name__ == '__main__':
  main()

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

mod = (10 ** 9) + 7
if abs(n - m) >= 2:
    print("0")
else:
    nn = math.factorial(n)
    mm = math.factorial(m)
    if abs(n - m) >= 1:
        print((nn * mm) % mod)
    else:
        ans = nn * 2 % mod
        ans = ans * mm % mod
        print(ans)

Solution (Python):
N = 10**6
mod = 10**9 + 7
fact = [1]
for i in range(1, N+1):
    fact.append(fact[-1]*i%mod)

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

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

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr


def lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


def my_factorial(n):
    ret = 1
    i = 1
    while i <= n:
        ret *= i
        i += 1
        ret %= mod
    return ret


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

Solution (Python):
def for_factorial(n):
    val = 1
    for i in range(2, n + 1):
        val *= i
        val %= (10 ** 9 + 7)
    return val
N, M = map(int, input().split())
if N == M:
    print(for_factorial(N) * for_factorial(M) * 2 % (10 ** 9 + 7))
elif abs(N-M) == 1:
    print(for_factorial(N) * for_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((math.factorial(n)**2)*2 % 1000000007)
else:
  print(math.factorial(max(n,m))*math.factorial(min(n,m)) % 1000000007)

Solution (Python):
def resolve():
    '''
    code here

    '''
    import math

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

    deff = N - M

    if deff == -1:
        N, M = M, N

    if deff == 0:
        res = (math.factorial(N)**2 * 2) % (10**9+7)

    elif abs(deff) == 1:
        res = (math.factorial(N) **2 // N ) % (10**9+7)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    '''
    code here

    '''
    import math

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

    deff = N - M

    if deff == -1:
        N, M = M, N

    if deff == 0:
        temp = math.factorial(N)
        res = (temp**2 * 2) % (10**9+7)

    elif abs(deff) == 1:
        res = (math.factorial(N) **2 // N ) % (10**9+7)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

Solution (Python):
def resolve():
    '''
    code here

    '''
    import math

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

    deff = N - M

    if deff == 0:
        temp = math.factorial(N)
        res = (temp**2 * 2) % (10**9+7)

    elif abs(deff) == 1:
        res = (math.factorial(N) * math.factorial(M)) % (10**9+7)

    else:
        res = 0
    
    print(res)

if __name__ == "__main__":
    resolve()

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

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

def factorial(k):
    mod = 10**9+7
    ret = 1
    for i in range(1,k+1):
        ret = (ret * (i%mod))%mod
    return ret

if abs(n-m)==1:
    ans =  (factorial(n)*factorial(m))%(10**9+7)
else:
    ans = (factorial(n)*factorial(m)*2)%(10**9+7)

print(ans)

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

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

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


def C(m, n):
    t = m
    s1 = 1
    while t > (m - n):
        s1 = s1 * t
        t = t - 1

    s2 = 1

    t = n
    while t >= 1:
        s2 = s2 * t
        t = t - 1

    return int(s1 / s2)


def A(m, n):
    s = 1
    t = m

    while t > m - n:
        s = (s * t) % 1000000007
        t = t - 1

    return s


def calculate(maxValue, minValue):
    if maxValue > minValue + 1:
        return 0

    if maxValue == minValue:
        S = A(maxValue,maxValue)
        return  2 * S * S

    return A(maxValue,maxValue) * A(minValue,minValue)

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

print(result % 1000000007)

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


def prepare(n):
    global MOD
    modFacts = [0] * (n + 1)
    modFacts[0] = 1
    for i in range(n):
        modFacts[i + 1] = (modFacts[i] * (i + 1)) % MOD

    invs = [1] * (n + 1)
    invs[n] = pow(modFacts[n], MOD - 2, MOD)
    for i in range(n, 1, -1):
        invs[i - 1] = (invs[i] * i) % MOD

    return modFacts, invs


N, M = map(int, input().split())
modFacts, invs = prepare(N + M)

A, B = (N, M) if N >= M else (M, N)

if A - 1 > B:
    cnt = 0
else:
    varA = modFacts[A] % MOD
    varB = modFacts[B] % MOD
    if A == B:
        varB *= 2
    cnt = (varA * varB) % MOD
print(cnt)

Solution (Python):
N, M = map(int, input().split())
def mod_factorial(a, mod):
    ans = 1
    while a != 1:
        ans *= a
        ans = ans % mod
        a -= 1
    return ans

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

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

if tmp >= 2:
    ans = 0
elif tmp == 1:
    ans = math.factorial(n) * math.factorial(m)
else:
    ans = 2 * math.factorial(n) * math.factorial(m)

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

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

Solution (Python):
import sys
from io import StringIO
import unittest

import math

def resolve():
    n, m = map(int, input().split())
    div = 10**9 + 7

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

class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)
    def test_入力例_1(self):
        input = """2 2"""
        output = """8"""
        self.assertIO(input, output)
    def test_入力例_2(self):
        input = """3 2"""
        output = """12"""
        self.assertIO(input, output)
    def test_入力例_3(self):
        input = """1 8"""
        output = """0"""
        self.assertIO(input, output)
    def test_入力例_4(self):
        input = """100000 100000"""
        output = """530123477"""
        self.assertIO(input, output)

if __name__ == "__main__":
    
    resolve()

Solution (Python):

mod=10**9+7

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


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

if n==1 and m==1:
    print(2)
elif n==2 and m==1:
    print(2)
elif n==1 and m==2:
    print(2)
elif n==2 and m==2:
    print(8)
elif n+2<=m:
    print(0)
elif n+1==m:
    k=1
    for i in range(1,n):
        k=mul(k,i+1)
    for i in range(1,m):
        k=mul(k,i+1)
    print(k)
elif n==m:
    k=1
    for i in range(1,n):
        k=mul(k,i+1)
    for i in range(1,m):
        k=mul(k,i+1)
    k=mul(k,2)
    print(k)
elif n==m+1:
    k=1
    for i in range(1,n):
        k=mul(k,i+1)
    for i in range(1,m):
        k=mul(k,i+1)
    print(k)
elif n>=m+2:
    print(0)

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

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

if N == M:
    print((2* factorial(N) ** 2) % MOD)
elif N == M + 1:
    print((factorial(M)**2*N) % MOD)
elif M == N + 1:
    print((factorial(N)**2*M) % MOD)
else:
    print(0)

Solution (Python):
def test():
	mod = 10**9 + 7
	n, m = map(int, input().split())
	if abs(n-m) > 1:
		print(0)
		exit()
	i_n = n
	i_m = m
	np = 1
	mp = 1
	while True:
		np = (np%mod) * (i_n%mod)
		mp = (mp%mod) * (i_m%mod)
		i_n -= 1
		i_m -= 1
		if i_n < 1:
			i_n = 1
		if i_m < 1:
			i_m = 1
		if i_n == 1 and i_m == 1:
			break
	s = 2 if n == m else 1
	r = (mp*np*s) % (10**9 + 7)
	print(r)
test()

Solution (Python):
import math

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

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

def main():
    n, m = map(int, input().split())
    if (abs(n-m)) >= 2:
        print(0)
    else:
        
        np = 1
        for i in range(n,0,-1):
            np = (np * i) % large_p
        mp = 1
        for i in range(m,0,-1):
            mp = (mp * i) % large_p

        if abs(n-m) == 1:
            r = (np * mp) % large_p
            print(r)
        else:
            r = ((np * mp) * 2) % large_p
            print(r)

if __name__ == '__main__':
    main()

Solution (Python):
from math import factorial
large_p = 10**9 + 7

def main():
    n, m = map(int, input().split())
    if (abs(n-m)) >= 2:
        print(0)
    else:
        np = factorial(n)
        mp = factorial(m)

        if abs(n-m) == 1:
            r = (np * mp) % large_p
            print(r)
        else:
            r = ((np * mp) * 2) % large_p
            print(r)

if __name__ == '__main__':
    main()

Solution (Python):
def resolve():
    T,M=map(int,input().split())

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

    fact = [1] * (N + 1)
    rfact = [1] * (N + 1)
    r = 1
    for i in range(1, N + 1):
        fact[i] = r = r * i % MOD
    rfact[N] = r = pow(fact[N], MOD - 2, MOD)
    for i in range(N, 0, -1):
        rfact[i - 1] = r = r * i % MOD

    
    def perm(n, k):
        return fact[n] * rfact[n - k] % MOD

    if T==M:
        ans=perm(T,T)*perm(M,M)*2%MOD
    elif abs(T-M)==1:
        ans=perm(T,T)*perm(M,M)%MOD
    else:
        ans=0
    print(ans)





resolve()

Solution (Python):




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


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

    mod = 10 ** 9 + 7
    factorial = factorial_mod(N, mod) * factorial_mod(M, mod)

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


if __name__ == "__main__":
    main()

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

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

def modfact(n, m):
    res = 1
    for i in range(2, n + 1):
        res = res * i % m
    return res

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

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

Solution (Python):
N, M = list(map(int, input().split()))
s = 10**9+7
a = 1

if abs(N-M)>=2:
    print(0)
else:
    if abs(N-M):
        for i in range(1, min(N,M)+1):
            a = (a * i) % s
        b = (a * max(N,M)) % s
        print((a * b) % s)
    else:
        for i in range(1, N+1):
            a = (a * i) % s
        c = (a * a) % s
        print((c * 2) % s)

Solution (Python):
import math

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

if N == M:
    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 sys
sys.setrecursionlimit(10**6)
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
from operator import itemgetter
from fractions import gcd
mod = 10 ** 9 + 7
inf = float('inf')
ninf = -float('inf')
 

def ii(): return int(sys.stdin.readline().rstrip()) 
def mii(): return map(int,sys.stdin.readline().rstrip().split())
def limii(): return list(mii()) 
def lin(n:int): return [ii() for _ in range(n)]
def llint(n: int): return [limii() for _ in range(n)]

def ss(): return sys.stdin.readline().rstrip() 
def mss(): return sys.stdin.readline().rstrip().split()
def limss(): return list(mss()) 
def lst(n:int): return [ss() for _ in range(n)]
def llstr(n: int): return [limss() for _ in range(n)]





def modFact(n, p): 
    if n >= p: 
        return 0    
  
    result = 1
    for i in range(1, n + 1): 
        result = (result * i) % p  
  
    return result 

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

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

n,m=max(n,m),min(n,m)

if n!=m:
    p = modFact(n,mod)
    q = modFact(m,mod)
    print(mul(p,q))

else:
    p = modFact(n,mod)
    q = modFact(m,mod)
    w = mul(p,q)
    print((2*w)%mod)

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

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


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

if __name__ == '__main__':
    solve()

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

Solution (Python):
N,M=map(int,input().split())
MOD=int(1e9)+7
if abs(N-M)>1:
    print(0)
else:
    fn=1;fm=1;
    for i in range(2,N+1):
        fn=(fn*i)%MOD
    for i in range(2,M+1):
        fm=(fm*i)%MOD
    if abs(N-M)==0:
        print((fn*fm*2)%MOD)
    else:
        print((fn*fm)%MOD)

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

Solution (Python):
import math

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

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

Solution (Python):
from math import factorial    


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

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

n = factorial(N)%mod
m = factorial(M)%mod
ans = n*m%mod

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

Solution (Python):
M=10**9+7
n,m=map(int,input().split())
if n<m: m,n=n,m
if n-m>1: print(0); exit()
a=1
for i in range(m): a=a*-~i%M
print(a*a*[2,n][n-m]%M)

Solution (Python):
M=10**9+7
n,m=map(int,input().split())
if n<m: m,n=n,m
if n-m>1: print(0); exit()
a=1
for i in range(m):
  a=a*-~i%M
print(a*a*[2,n][n-m]%M)

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque


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

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

print(ans)

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

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

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

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((math.factorial(n)%mod * math.factorial(m)%mod)*2%mod)
  else:
    print((math.factorial(n)%mod * math.factorial(m)%mod)%mod)

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

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

g = 0
if n == m:
    g = 2
if abs(n - m) == 1:
    g = 1

N = 10 ** 5 + 10
fact = [0] * (N)
fact[0] = 1
for i in range(1, N):
    fact[i] = (fact[i-1] * i) % mod

print(g * fact[n] * fact[m] % mod)

Solution (Python):
from math import factorial

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

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

Solution (Python):
from math import factorial

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

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

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

if N==M:
    print((2*((factorial(N))%P)*((factorial(M))%P))%P)
elif min(N,M)+1==max(N,M):
    print((((factorial(N))%P)*((factorial(M))%P))%P)
else:
    print("0")

Solution (Python):
import sys
from math import factorial
sys.setrecursionlimit(100000)
def modfact(x,p):
    if x==1:
        return 1
    else:
        return (x%p)*modfact(x-1,p)
N,M=map(int,input().split())
P=10**9+7
"""
if N==M:
    print((2*modfact(N,P)*modfact(M,P))%P)
elif min(N,M)+1==max(N,M):
    print((modfact(N,P)*modfact(M,P))%P)
else:
    print("0")
"""
if N==M:
    print((2*((factorial(N))%P)*((factorial(M))%P))%P)
elif min(N,M)+1==max(N,M):
    print((((factorial(N))%P)*((factorial(M))%P))%P)
else:
    print("0")

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

Solution (Python):
from math import factorial

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

if abs(n-m) >= 2:
    print(0)
else:
    n_per = factorial(n) // factorial(n - n)
    m_per = factorial(m) // factorial(m - m)
    if n != m:
        print((n_per * m_per) % (10**9 + 7))
    else:
        print((n_per * m_per *2) % (10**9 + 7))

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

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


MOD = 10 ** 9 + 7
class modint:
    def __init__(self, x):
        self.x = x.x if isinstance(x, modint) else x % MOD
    def __str__(self): return str(self.x)
    __repr__ = __str__
    def __int__(self): return self.x
    __index__ = __int__
    def __add__(self, other): return modint(self.x + modint(other).x)
    def __sub__(self, other): return modint(self.x - modint(other).x)
    def __mul__(self, other): return modint(self.x * modint(other).x)
    def __pow__(self, other): return modint(pow(self.x, modint(other).x, MOD))
    def __floordiv__(self, other): return modint(self.x * pow(modint(other).x, MOD - 2, MOD))
    def __eq__(self, other): return self.x == modint(other).x
    def __ne__(self, other): return self.x != modint(other).x
    def __inv__(self): return pow(self.x, MOD - 2, MOD)

def fact(n):
    r = n
    for i in range(n - 1, 1, -1):
        r *= i
    return r

fn = fact(modint(N))
fm = fact(modint(M))

if N == M:
    print(fn * fm * 2)
else:
    print(fn * fm)

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



def factorial(n,mod):
    fac = [0]*(n+1)
    fac[0] = fac[1] = 1
    for i in range(2,n+1):
        fac[i] = fac[i-1]*i % mod
    return fac

fs = factorial(max(N,M),MOD)

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

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	n, m = IN()
	if abs(n-m) > 1:
		return 0
		
	(n,m) = (n,m) if n<=m else (m,n)
	pn = 1
	for i in range(1,n+1):
		pn=pn*i%mod
	if n==m:
		pn = (pn * pn * 2) % mod
	else:
		pn = (pn * pn * m) % mod
	return pn
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

Solution (Python):
nm = list(map(int, input().split()))
mod = 10 ** 9 + 7
def fact(a):
  ret = 1
  while a > 0:
    ret = ret * a % mod
    a -= 1
  return ret

nm.sort()
if nm[1] - nm[0] > 1:
  print(0)
elif nm[1] - nm[0] == 1:
  print(fact(nm[1]) * fact(nm[0]) % mod)
else:
  print(2* fact(nm[1]) * fact(nm[0]) % mod)

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

INF = 10**9 + 7

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

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

def mod_factorial(n):
  ret = 1
  for i in range(1, n+1):
    ret = ret * i % mod
  return ret
  
def main():
  n, m = map(int, input().split())
  if abs(n-m) > 1:
    return 0
  
  l = min(n, m)
  f = mod_factorial(l)
  
  if n == m:
    return(2*f*f%mod)
  else:
    return(f*f*(l+1)%mod)

print(main())

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

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

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

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


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

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

from math import factorial
ans = 0
div = int(1e9 + 7)

diff = abs(N - M)
if diff < 2:
  x = factorial(N) % div
  y = factorial(M) % div
  ans = (x * y) % div

if diff == 0:
  ans = (ans * 2) % div
print(ans)

Solution (Python):
import math

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

rev = 10**9+7

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

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

ansN=1
ansM=1

if N-1==M or N==M-1 or N==M:
    for i in range(2,N+1):
        ansN*=(i%mod)
        ansN%=mod
    for g in range(2,M+1):
        ansM*=(g%mod)
        ansM%=mod
    if N==M:
        print((ansM*ansN*2)%mod)
    else:
        print((ansM*ansN)%mod)
else:
    print(0)

Solution (Python):
def main():
  import math 
  mf=math.factorial
  n,m=map(int,input().split())
  mod=pow(10,9)+7
  print(0 if abs(n-m)>=2 else ((mf(n)%mod)*(mf(m)%mod)*(2 if abs(n-m)==0 else 1))%mod)
if __name__=='__main__':
  main()

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

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

import sys,fractions
N,M=map(int,input().split(' '))

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


frac_n = frac(N)
frac_m = frac(M)

if N == M:
  ans = frac_m * frac_n * 2
else:
  ans = frac_n * frac_m

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

Solution (Python):

import sys

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


divisor = 10**9 + 7

cnt=1
diff = abs(N-M)
if diff >=2:
    print("0")
    sys.exit()
else:
    for num in [N,M]:
        for i in range(1,num+1):
            cnt = (cnt * i) % divisor

    if diff == 0:
        cnt = (cnt * 2) % divisor

print(cnt)

Solution (Python):

from functools import reduce
import sys

MOD = 1000000007  


def solve(N: int, M: int):
    if M > N:
        M, N = N, M
    if M + 1 < N:
        print(0)
        return
    mf = reduce(lambda a, b: a * b % MOD, range(1, M + 1), 1)
    if M == N:
        ans = mf * mf % MOD * 2 % MOD
    else:
        ans = mf * mf % MOD * N % MOD
    print(ans)


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()