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



Sample input:
2 2


Sample output:
8

Submission status: Time Limit Exceeded

Solution code:
def f(x):
    k = 1
    for i in range(1,x+1):
        k *= i
    return k % (10**9 + 7)

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

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

Submission status: Time Limit Exceeded

Solution code:
import math

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

Submission status: Time Limit Exceeded

Solution code:

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

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

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

Submission status: Time Limit Exceeded

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

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

elif N == M:
	answer = 2
	for i in range(1, N+1):
		answer = answer * i * i

	answer = answer % (1000000000 + 7)
	print (answer)

elif N > M:
	answer = 1
	j = 1
	for i in range(2, N+1):
		answer = answer * i * j
		j = i

	answer = answer % (1000000000 + 7)
	print(answer)

else:
	answer = 1
	j = 1
	for i in range(2, M+1):
		answer = answer * i * j
		j = i

	answer = answer % (1000000000 + 7)
	print(answer)

Submission status: Time Limit Exceeded

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

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

L = 0
if N == M:
    cnt = 1
    for i in range(1,N+1):
        cnt *= i
    ccnt = cnt
else:
    mi = min([N,M])
    ma = max([N,M])
    cnt = 1
    for i in range(1,mi+1):
        cnt *= i

    ccnt = cnt * ma


nn = N%2
mm = M%2
if (nn == 0 and mm == 0) or (nn == 1 and mm == 1):
    print((cnt * ccnt * 2) % 1000000007)
else:
    print((cnt * ccnt) % 1000000007)

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().strip().split(' ')))
k = abs(N-M)
def kaijou(n):
  if n == 0:
    return(1)
  else:
    c = 1
    while n != 1:
      c *= n
      n -= 1
    return(c)
if k > 1:
  print(0)
elif k == 1:
  ans = (kaijou(N)*kaijou(M))%(10**9+7)
  print(ans)
elif k == 0:
  ans = (kaijou(N)*kaijou(M)*2)%(10**9+7)
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import math as m
a,b=map(int,input().split())
print(m.factorial(a)*m.factorial(b)*[1,2][a==b] if -2<a-b<2 else 0)

Submission status: Time Limit Exceeded

Solution code:
N, M = map(int, input().split())
ans = 0
nn = 1
mm = 1
for i in range(1, N+1):
    nn *= i
for i in range(1, M+1):
    mm *= i

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

elif abs(N-M) ==1:
    ans = ((nn*mm)%(10**9+7))

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(250010)
N,M=map(int, input().split())
def func(n):
    if n==1:
        return 1
    return n*func(n-1)

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

Submission status: Time Limit Exceeded

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

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

if max(n,m) > min(n,m):
	print math.factorial(max(n,m)) % mod * math.factorial(min(n,m)) % mod

else :
	print (((math.factorial(n))%mod)**2 * 2)%mod

Submission status: Time Limit Exceeded

Solution code:
import math
n,m = map(int,raw_input().split())

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

if max(n,m) > min(n,m):
	print math.factorial(max(n,m)) * math.factorial(min(n,m)) % (10**9 + 7)

else :
	print (math.factorial(n)**2 * 2)%(10**9 + 7)

Submission status: Time Limit Exceeded

Solution code:
nums = [1, 1]
def step_div(n):
	i = len(nums)
	while i <= n:
		nums.append(nums[i-1] * i)
		i += 1
	return nums[n]

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

remain = abs(n-m)
if remain <= 1:
	print((step_div(n) * step_div(m) * (2 if remain==0 else 1)) % (10**9+7))
else:
	print(0)

Submission status: Time Limit Exceeded

Solution code:
nums = [1, 1]
def step_div(n):
	i = len(nums)
	while i <= n:
		nums.append(nums[i-1] * i)
		i += 1
	return nums[n]

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

remain = abs(n-m)
if remain <= 1:
	print((step_div(n % (10**9+7)) * step_div(m % (10**9+7)) * (2 if remain==0 else 1)) % (10**9+7))
else:
	print(0)

Submission status: Time Limit Exceeded

Solution code:
def Factnal(n):
    x = 1;
    for i in range(1,n + 1):
        x *= i
    return x    
x,y = map(int,input().split(' '))
if abs(x - y) > 1:
    print(0)
elif x == y:
    print(2 * Factnal(x) * Factnal(y) % (10 ** 9 + 7))
else:
    minV = min(x,y);
    print(Factnal(minV + 1) * Factnal(minV) % (10 ** 9 + 7))

Submission status: Time Limit Exceeded

Solution code:
def Factnal(n):
    x = 1;
    for i in range(1,n + 1):
        x *= i
    return x    
x,y = map(int,input().split(' '))
if abs(x - y) > 1:
    print(0)
elif x == y:
    print(2 * Factnal(x) * Factnal(y))
else:
    minV = min(x,y);
    print(Factnal(minV + 1) * Factnal(minV))

Submission status: Time Limit Exceeded

Solution code:
n, m = map(int, input().split())
mod = 10**9 + 7
def fact(n):
  i = n
  ret = 1
  while i > 1:
    ret *= i
    i -= 1
  return ret
if n == m:
  print(fact(n) ** 2 * 2 % mod)
# elif n+1 == m or n-1 == m:
#   print(fact(n) * fact(m))
elif n == m+1:
  n_f = fact(n)
  print(n_f * (n_f//n) % mod)
elif n == m-1:
  m_f = fact(m)
  print(m_f * (m_f//m) % mod)
else:
  print(0)

Submission status: Time Limit Exceeded

Solution code:
import sys

res=10**9+7
def permutation(n):
  x=1
  for i in range(n):x=(x*(i+1))
  return(x)
n,m=map(int,input().split())
n%=res
m%=res
if(n>m+1 or m>n+1):
  print(0)
elif(n==m):
  p=permutation(n)
  print((2*p*p)%res)
else:
  ma=max(n,m)
  p=permutation(ma)
  p2=p//ma
  print((p*p2)%res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
mod=10**9+7
sn=1
sm=1
for i in range(1,n+1):
    sn*=i
for j in range(1,m+1):
    sm*=j
if 2<=abs(n-m):
    print(0)
elif abs(n-m)==1:
    print(sn*sm%mod)
elif n==m:
    print(2*sn*sm%mod)

Submission status: Time Limit Exceeded

Solution code:
import functools
import operator

N, M = map(int, input().split())
sub = abs(N - M)

def main(N, M, sub):
    if sub > 1:
        # 解なし
        print("0")
    else:
        m = max(N, M)
        n = min(N, M)

        per_a = 1
        for i in range(n):
            per_a *= (i+1)

        per_b = per_a
        for i in range(n, m):
            per_b *= (i+1)

        if sub == 1:
            ans = (per_a * per_b) % (pow(10, 9)+7)
            print(ans)
        else:
            ans = (per_a * per_b * 2) % (pow(10, 9)+7)
            print(ans)

main(N, M, sub)

Submission status: Time Limit Exceeded

Solution code:
a, b = [int(i) for i in input().split()]
num = 1
numb = 1
if abs(a-b) == 1:
    for i in range(a):
        num = (a-i)*num
    for i in range(b):
        numb = (b-i)*numb
    num = num*numb
elif abs(a-b) == 0:
    for i in range(a):
        num = (a-i)*num
    num = (num**2)*2
else:
    num = 0

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

Submission status: Time Limit Exceeded

Solution code:
def kai(x):
    k=1
    for i in range(1,x+1):
        k*=i
    return k

a,b=map(int,input().split())
A=kai(a)
B=kai(b)

import sys

ans=0
if a==b+1 or b==a+1:
    ans=A*B
elif b==a:
    ans=2*A*B
else:
    print(0)
    sys.exit()
print(ans%(10**9+7))

Submission status: Time Limit Exceeded

Solution code:
def kai(x):
    k=1
    for i in range(1,x+1):
        k*=i
    return k

a,b=map(int,input().split())
A=kai(a)
B=kai(b)

import sys

ans=0
if a==b+1 or b==a+1:
    ans=A*B%(10**9+7)
elif b==a:
    ans=2*A*B%(10**9+7)
else:
    print(0)
    sys.exit()
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def kai(x):
    k=1
    for i in range(1,x+1):
        k*=i
    return k

import sys
a,b=map(int,input().split())
ans=0
if a==b+1 or b==a+1:
    ans=kai(a)*kai(b)%(10**9+7)
elif b==a:
    ans=2*kai(a)*kai(b)%(10**9+7)
else:
    print(0)
    sys.exit()
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import sys
l = raw_input().split()
n = int(l[0])
m = int(l[1])

result = 1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
l = raw_input().split()
n = int(l[0])
m = int(l[1])

result = 1

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

Submission status: Time Limit Exceeded

Solution code:
import sys
l = raw_input().split()
n = int(l[0])
m = int(l[1])

def factorial(n):
    assert n >= 1
    result = 1
    for i in range(1,n+1):
        result *= i
    return result

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
n,m = map(int, raw_input().split())
mod = 10**9+7
if(n==m):
    print((2*(math.factorial(n))**2)%mod)
elif(abs(n-m)==1):
    tmp = math.factorial(min(n,m))
    print((max(n,m)*(tmp**2)%mod))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
from math import factorial

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

Submission status: Time Limit Exceeded

Solution code:
def factorial(n) : 
    a = 1
    for i in range(1 , n+1):
        a *= i
    return a

def solve():
    (n,m) = map(int,input().split())
    if(abs(n-m)>1):
        return 0
    if(n==m):
        return (factorial(n)**2 * 2) % 1000000007
    if(n > m):
        return (factorial(m)**2 * n) % 1000000007
    else:
        return (factorial(n)**2 * m) % 1000000007

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

Submission status: Time Limit Exceeded

Solution code:
def factorial(integer) : 
    product = 1
    if integer == 0 : return 1
    for num in range(1 , integer + 1):
        product *= num
    return product

def solve():
    (n,m) = map(int,input().split())
    if(abs(n-m)>1):
        return 0
    if(n==m):
        return (factorial(n) * factorial(m) * 2) % 1000000007
    return (factorial(n) * factorial(m)) % 1000000007

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

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    a = 1
    for i in range(1, n+1):
        a *= i
    return a

def solve():
    (n,m) = map(int,input().split())
    if(abs(n-m)>1):
        return 0
    if(n==m):
        return (f(n) * f(m) * 2) % 1000000007
    return (f(n) * f(m)) % 1000000007

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

Submission status: Time Limit Exceeded

Solution code:
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))
else:
    print((math.factorial(N) ** 2 * 2) % (10 ** 9 + 7))

Submission status: Time Limit Exceeded

Solution code:
import math
N,M=list(map(int,input().split()))
x=math.factorial(N)
y=math.factorial(M)
if(abs(N-M)>1):
    print(0)
else:
    if((N%2==0 and M%2==0) or N==M):
        print(x*y*2%(10**9+7))
    else:
        print(x*y)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2 ** 30)

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

N, M=map(int,input().split())
if(abs(N%1000000007-M%1000000007)>1):
    i=0
else:
    i=fact(M%1000000007)*fact(N%1000000007)
if(N==M):
    i=i*2
ans=i%1000000007
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2 ** 30)

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


N, M=map(int,input().split())
if(abs(N%1000000007-M%1000000007)>1):
    i=0
else:
    i=fact(M%1000000007)*fact(N%1000000007)
if(N==M):
    i=i*2
ans=i%1000000007
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def fct(x):
	t = long(1)
	for i in range(1, x+1):
		t = t * long(i)
	return t

inpt = map(int, raw_input().split())
n = inpt[0]
m = inpt[1]

ans = long(0)

if abs(n-m) < 2:
	ans = fct(n)*fct(m)
	if n == m:
		ans = 2 * ans

print ans

Submission status: Time Limit Exceeded

Solution code:
mode=1000000007

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


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

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

Submission status: Time Limit Exceeded

Solution code:
N,M = list(map(int,input().split()))
a = 10**9 + 7
def fact(n):
    f = 1
    for i in range(1,n+1):
        f*=i
    return f
if(abs(N-M)>1):print(0)
elif(N > M):
    f = fact(M)
    print(int(N*(f**2)%a))
elif(N < M):
    f = fact(N)
    print(int(M*(f**2)%a))
elif(N == M):
    f = fact(N)
    print(int(((f**2)*2)%a))

Submission status: Time Limit Exceeded

Solution code:
N,M = list(map(int,input().split()))
a = 10**9 + 7
def fact(n):
    f = 1
    for i in range(1,n+1):
        f*=i
    return f
if(abs(N-M)>1):print(0)
elif(N != M):
    print(int((fact(N)*fact(M))%a))
elif(N == M):
    print(int((fact(N)*fact(M)*2)%a))

Submission status: Time Limit Exceeded

Solution code:
N,M = list(map(int,input().split()))
a = 10**9 + 7
def fact(n):
    f = 1
    for i in range(1,n+1):
        f*=i
    return f
if(abs(N-M)>1):print(0)
elif(N != M):
    minc = fact(min(N,M))
    maxc = fact(max(N,M))
    print(int((minc*maxc)%a))
elif(N == M):
    minc = fact(N)
    maxc = fact(N)
    print(int((minc*maxc*2)%a))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

max_num, min_num = (N, M) if N >= M else (M, N)
if max_num - min_num == 1:
    pattern = len(list(itertools.permutations(range(max_num), max_num))) * \
              len(list(itertools.permutations(range(min_num), min_num)))
elif max_num - min_num == 0:
    pattern = len(list(itertools.permutations(range(max_num), max_num))) * \
              len(list(itertools.permutations(range(min_num), min_num))) * 2
else:
    pattern = 0
    
print(pattern)

Submission status: Time Limit Exceeded

Solution code:
import math

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

if math.fabs(N - M) > 1:
  print(0)

elif N - M == 0:
  n = 1
  for i in range(1, N + 1):
    n = n * i

  m = 1
  for j in range(1, M + 1):
    m = m * j
  print((n * m * 2) % 1000000007)

else:
  n = 1
  for i in range(1, N + 1):
    n = n * i

  m = 1
  for j in range(1, M + 1):
    m = m * j

  print((n * m) % 1000000007)

Submission status: Time Limit Exceeded

Solution code:
def fact(n):
    x = 1
    while(True):
        x *= n
        n -= 1
        if(n <= 0):
            return x


max = 10 ** 9 + 7
N, M = map((int), input().split() )
if(abs(N - M) > 1):
    print(0)
    exit()
dog = fact(N)
monkey = fact(M)
if(N == M):
    print((2 * dog * monkey) % max)
else:
    print((dog * monkey) % max)

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))
cons = (10**9+7)
if N == M:
    s = 1
    for i in range(2, N+1):
        s *= i
    print((s**2*2)%cons)

elif abs(N-M) == 1:
    N = min(N, M)
    s = 1
    for i in range(2, N+1):
        s *= i
    print((s*(s*(N+1))%cons))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
#
#すぬけ君は、犬を N 匹と猿を M 匹飼っています。
#すぬけ君は、この N+M 匹を一列に並べようと思っています。
#文字通り犬猿の仲の犬たちと猿たちを仲直りさせたいすぬけ君は、
#犬同士、または猿同士が隣り合うところができないように並べようと思っています。
#このような並べ方は何通りあるでしょうか。
#犬と猿は 109+7 以上の数を理解できないので、
#並べ方の個数を 109+7 で割ったあまりを求めてください。 
#ただし、犬同士、また猿同士は互いに区別します。また、左右が反転しただけの列も異なる列とみなします。

Data = input().split()
N = int(Data[0])
M = int(Data[1])


if abs(N - M) >= 2:
    print(0)
    
else:
    A = 1
    B = 1
    for i in range(N):
        A = A * (i + 1)

    for i in range(M):
        B = B * (i + 1)

    if N == M:
        ans1 = A * B * 2
    else:
        ans1 = A * B
    
    ans2 = ans1 % (1000000000 + 7)
    
    print(ans2)

Submission status: Time Limit Exceeded

Solution code:
N, M = list(map(int, input().split()))
cons = (10**9+7)
if N == M:
    s = 1
    for i in range(2, N+1):
        s *= i%cons
    print((s**2*2)%cons)

elif abs(N-M) == 1:
    N = max(N, M)
    s = 1
    for i in range(2, N+1):
        s *= i%cons
    print((s*(s//N)%cons))
else:
    print(0)

Submission status: Time Limit Exceeded

Solution code:
S = input().split()

N = int(S[0])
M = int(S[1])
sumN=1
sumM=1
num=10**9+7

if N == M :
    for i in range(N):
        sumN = sumN * N
        sumM = sumM * M
        N = N - 1
        M = M - 1
    print(2*sumN*sumM%num)
elif abs(N-M) == 1:
    for i in range(N):
        sumN = sumN * N
        N = N - 1
    for i in range(M):
        sumM = sumM * M
        M = M - 1
    print(sumN*sumM%num)
else :
    print("0")

Submission status: Time Limit Exceeded

Solution code:
#coding: utf-8
import sys
import math
N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
    sys.exit()
elif N == M:
    ans = math.factorial(N) * math.factorial(M) * 2
else:
    ans = math.factorial(N) * math.factorial(M)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,M = map(int,raw_input().split())
_sum = 1
if abs(N-M) >1:
    print 0
elif N == M:
    for i in xrange(1,N+1):
        _sum *= i*i
    print 2*_sum%(10**9+7)
else:
    val = M if M < N else N
    for i in xrange(1,val+1):
        _sum *= i*i
    _sum *= (val+1)
    print _sum%(10**9+7)

Submission status: Memory Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000)

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

@lru_cache(maxsize=128)
def factorial(n):
  if n in {0,1}:
    return 1
  return n*factorial(n-1) % 1000000007

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

Submission status: Memory Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000)

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

@lru_cache(maxsize=512)
def factorial(n):
  if n in {0,1}:
    return 1
  return n*factorial(n-1) % 1000000007

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

Submission status: Memory Limit Exceeded

Solution code:
from functools import lru_cache
import sys
sys.setrecursionlimit(1000000007)

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

@lru_cache(maxsize=1024)
def factorial(n):
  if n in {0,1}:
    return 1
  return n*factorial(n-1) % 1000000007

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

Submission status: Memory Limit Exceeded

Solution code:
import sys
from functools import lru_cache
sys.setrecursionlimit(1000000)
a = list(map(int, input().split()))


@lru_cache(maxsize=None)
def stair(n):
    if n == 1:
        return 1
    else:
        return (n * stair(n - 1)) % 1000000007
if abs(a[0] - a[1]) > 1:
    print("0")
elif abs(a[0] - a[1]) == 1:
    print((stair(a[0]) * stair(a[1]) % 1000000007))
else:
    print((stair(a[0]) * stair(a[1]) * 2)% 1000000007)

Submission status: Accepted

Solution code:
from math import factorial as f
n,m=map(int,input().split())
if abs(n-m) >= 2:
  print(0)
  quit()
if n == m:
  print(f(n)*f(m)*2%(10**9+7))
else:
  print(f(max(m,n))*f(min(m,n))%(10**9+7))

Submission status: Accepted

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

def factorial(n):
    ans = 1
    i = 0
    while i < n:
        ans *= (n-i)
        ans %= mod
        i += 1
    return ans

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 27 02:58:21 2020

@author: liang
"""

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

if abs(N-M) >= 2:
    ans = 0
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
    
    if N == M:
        ans *= 2
        ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
if n > m:
    n,m = m,n
ans = 1
if m==n:

    while n > 0:
        ans *= n
        ans %= 10**9+7
        n -= 1
    ans = (2*(ans**2))%(10**9+7)
    print(ans)

elif m - n == 1:
    while n > 0:
        ans *= n
        ans %= 10**9+7
        n -= 1
    
    
    ans = (ans * (ans*m % (10**9+7))) % (10**9+7)
    print(ans)
else:
    print(0)

Submission status: Accepted

Solution code:
def factorial(x, mod=10**9+7):
    v = 1
    for i in range(2, x+1):
        v *= i
        v %= mod
    return v


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

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

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

print(ans)

Submission status: Accepted

Solution code:
MOD =  pow(10,9)+7
def MODINV(n:int, MOD=MOD):
    return pow(n, MOD-2, MOD)

def perm(n, k, MOD):
    numer = 1
    for i in range(n, n-k, -1):
        numer *= i
        numer %= MOD
    return numer

def main():
    N, M = map(int, input().split())
    if N == M+1 or N+1 == M:
        ans = (perm(N, N, MOD)*perm(M, M, MOD))%MOD
    elif N == M:
        ans = (2*(perm(N, N, MOD)*perm(M, M, MOD)))%MOD
    else:
        ans = 0
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
from math import factorial as fac

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

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

Submission status: Accepted

Solution code:
n,m = map(int, input().split())
f = [1] 
p = 10**9 + 7
for i in range(1, max(n,m) + 1):
  f.append(f[-1] * i % p)

if abs(n-m) == 1:
  print(f[n] * f[m] % p)
elif abs(n-m) == 0:
  print(f[n] * f[m] * 2 % p)
else:
  print(0)

Submission status: Accepted

Solution code:
MOD = 10 ** 9 + 7

kai = [1, 1]
for i in range(2, 10 ** 5 + 1):
    num = kai[-1] * i
    kai.append(num % MOD)
n, m = map(int, input().split())
if abs(n - m) > 1:
    ans = 0
else:
    ans = kai[n] * kai[m]
    if n == m:
        ans *= 2
    ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import math
n,m = map(int,input().split())
mod = 10**9+7
fact_n = math.factorial(n) % mod
fact_m = math.factorial(m) % mod

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

Submission status: Accepted

Solution code:
import math
n,m = map(int,input().split())
mod = 10**9+7
fact_n = math.factorial(n) % mod
fact_m = math.factorial(m) % mod

if abs(n-m) >= 2:
  print(0)
  exit()
elif n == m:
  print((fact_n * fact_m * 2) % mod)
else:
  print((fact_n * fact_m) % mod)

Submission status: Accepted

Solution code:
# C - Reconciled?
def main():
    from math import factorial

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

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

if __name__ ==  "__main__":
    main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mod = 1000000007

if abs(N - M) > 1:
    print(0)
elif (N + M) % 2 == 1:
    x = max(N, M)
    y = min(N, M)
    Sum = 1
    for i in range(N + M):
        if i % 2 == 0:
            Sum *= x - (i // 2)
            Sum %= mod
        else:
            Sum *= y - (i // 2)
            Sum %= mod
    print(Sum)
else:
    Sum = 1
    for i in range(2 * N):
        Sum *= N - (i // 2)
        Sum %= mod
    Sum *= 2
    Sum %= mod
    print(Sum)

Submission status: Accepted

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

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

Submission status: Accepted

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


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


mod = 10**9+7
mn = min(m, n)
mx = max(m, n)
if abs(mn-mx) > 1:
    ans = 0
else:
    if n == m:
        ans = (2*facmod(m, mod)*facmod(n, mod)) % mod
    else:
        ans = (facmod(mx, mod)*facmod(mn, mod)) % mod
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

#l = list(map(int, input().split()))
#import numpy as np
#arr = np.array([int(i) for i in input().split()])
'''
a,b=[],[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''

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

f=[1]
for i in range(1,max(n,m)+1):
    x=f[i-1]*i
    if x>10**9+7:
        x%=10**9+7
    f.append(x)
if n!=m:
    print(f[n]*f[m]%(10**9+7))
else:
    print(f[n]*f[m]*2%(10**9+7))

Submission status: Accepted

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



nf = math.factorial(n) % p
mf = math.factorial(m) % p

if abs(n-m) > 1:
  ans = 0

elif n == m:
  ans = nf * mf * 2
  
else:
  ans = nf * mf
  
print(ans % p)

Submission status: Accepted

Solution code:
import math
N,M = map(int,input().split())
f = 1
if abs(N-M) > 1:
    print(0)
    exit()
if (N+M) % 2 == 0:
    f = 2

print(math.factorial(N) * math.factorial(M) * f % (10**9+7))

Submission status: Accepted

Solution code:
import math
N,M = map(int,input().split())
f = 1
if abs(N-M) > 1:
    print(0)
    exit()
if (N+M) % 2 == 0:
    f = 2

print(math.factorial(N) % (10**9+7) * math.factorial(M) % (10**9+7) * f % (10**9+7))

Submission status: Accepted

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


if N-1 <= M <= N+1:
    ans = 1
    if N == M:
        for _ in range(2):
            for i in range(1, N+1):
                ans = ans * i % NUM
        ans = ans * 2 % NUM
    else:
        for i in range(1, N+1):
            ans = ans * i % NUM
        for i in range(1, M+1):
            ans = ans * i % NUM
else:
    ans = 0

print(ans)

Submission status: Accepted

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


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

    if abs(n - m) >= 2:
        ans = 0
    elif abs(n - m) == 1:
        ans = (factorial(n, md) * factorial(m, md))
        ans %= md
    else:
        ans = (factorial(n, md) * factorial(m, md)) * 2
        ans %= md

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque,defaultdict,Counter
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from bisect import bisect_left,bisect_right
from math import sqrt,gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint
from statistics import mean,median,mode
import sys
sys.setrecursionlimit(10 ** 6)

INF = float('inf')

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse=False):
    data.sort(key=lambda x:x[col],reverse=revese)
    return data

def mymax(data):
    M = -1*float('inf')
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float('inf')
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def mydictvaluesort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[1] )

def mydictkeysort(dictionary):
    return sorted( dictionary.items(), key=lambda x:x[0] )

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(' ')
        elif type(ls[0])==str:
            print(' '.join(ls))
        elif type(ls[0])==int:
            print(' '.join(map(str,ls)))
        else:
            print('Output Error')
    else:
        if len(ls)==0:
            print('')
        elif type(ls[0])==str:
            print(''.join(ls))
        elif type(ls[0])==int:
            print(''.join(map(str,ls)))
        else:
            print('Output Error')

def I():
    return int(input())

def MI():
    return map(int,input().split())

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

def CI(n):
    return [ int(input()) for _ in range(n) ]

def LI(n):
    return [ list(map(int,input().split())) for _ in range(n) ]

def S():
    return input()

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

def RS():
    return list(input())

def CS(n):
    return [ input() for _ in range(n) ]

def LS(n):
    return [ list(input()) for _ in range(n) ]

# ddict = defaultdict(lambda: 0)
# ddict = defaultdict(lambda: 1)
# ddict = defaultdict(lambda: int())
# ddict = defaultdict(lambda: list())
# ddict = defaultdict(lambda: float())

mod = 10**9 + 7

n,m = MI()

a = factorial(n)%mod
b = factorial(m)%mod

if n==m: 
    ans = a*b*2 % mod
elif (n+1)==m or (m+1)==n:
    ans = a*b % mod
else:
    ans = 0

print(ans)