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: Accepted

Solution code:
MOD = 1000000007

dp=[0 for _ in range(100001)]
dp[0]=1

for i in range(1, 100001):
    dp[i] = dp[i-1]*i%MOD


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

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split(" "))
(n, m) = (n, m) if n>m else (m, n)
M=1000000007

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

if n > m+1:
    print(0)
elif n == m:
    print((2 * newfact(n) * newfact(m)) % M)
else:
    print((newfact(n) * newfact(m)) % M)

Submission status: Accepted

Solution code:
# encoding:utf-8
import math

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

if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
from functools import reduce
a, b = map(int, input().split() )
aa = reduce(lambda y,x: y*x%1000000007, [i for i in range(1,a+1) ] )
bb = reduce(lambda y,x: y*x%1000000007, [i for i in range(1,b+1) ] )
if a == b :
  print(2*aa*bb%1000000007)
elif a+1 == b or a == b+1:
  print(aa*bb%1000000007)
else:
  print(0)

Submission status: Accepted

Solution code:
from collections import defaultdict
from math import factorial
MOD = 10 ** 9 + 7


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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
MOD = int(1e9 + 7)

def fact(n):
    ret = 1
    for i in range(n):
        ret = (ret * (i + 1)) % MOD
    return ret

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
data = [int(n) for n in input().split()]
LIM = 10**9 + 7

if (data[0] - data[1] > 1) or (data[1] - data[0] > 1):
    print(0)
else:
    a = max(data[0], data[1])
    b = min(data[0], data[1])

    tmp = 1
    for i in range(a):
        tmp = (tmp * (i+1)) % LIM
    for i in range(b):
        tmp = (tmp * (i+1)) % LIM
    if a == b:
        tmp = (2 * tmp) % LIM

    print(tmp)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def fact(x):
    f = 1
    for i in range(1,x+1):
        f *= i
        f %= 10**9+7
    return f             

n,m = map(int, raw_input().split())
mod = 10**9+7
if(n==m):
    print((2*(fact(n))**2)%mod)
elif(abs(n-m)==1):
    tmp = fact(min(n,m))
    print(max(n,m)*(tmp**2)%mod)
else:
    print(0)

Submission status: Accepted

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(n) * factorial(m) % 1000000007)
else:
    print(0)

Submission status: Accepted

Solution code:
import math

mod = 1000000007

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

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

Submission status: Accepted

Solution code:
#Reconciled?
import numpy as np
import math as mt
N,M = list(map(int, input().split()))
if(abs(N-M)>1):
    a=0
else:
    if (N==M):
        a = 2*mt.factorial(N)**2
    if (N!=M):
        a = mt.factorial(N)*mt.factorial(M)
print(a%(10**9+7))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
a = raw_input().split()
for i in range(2):
	a[i] = int(a[i])
seki1 = 1
seki2 = 1
if (a[0] - a[1] == 1 or a[0] -a[1] == -1):
	for i in range(a[0]):
		seki1 *= i+1
		seki1  = seki1 % (10**9+7)
	for i in range(a[1]):
		seki2 *= i+1
		seki2  = seki2 % (10**9+7)
	ans = (seki1*seki2) % (10**9 + 7)
	print ans
	
elif a[0] == a[1]:
	for i in range(a[0]):
		seki1 *= i+1
		seki1  = seki1 % (10**9+7)

	ans = (2*seki1*seki1) % (10**9 + 7)
	print ans
	
else:
	print 0

Submission status: Accepted

Solution code:
def factmod(n):
    a = 1
    d = 1
    while d <= n:
        a *= d
        a %= 10**9+7
        d += 1
    return a

n,m = map(int, raw_input().split())
d = abs(n-m)
if d == 0:
    print (factmod(n) * factmod(m) * 2) % (10**9 + 7)
elif d == 1:
    print (factmod(n) * factmod(m)) % (10**9 + 7)
else:
    print 0

Submission status: Accepted

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

Submission status: Accepted

Solution code:
M=10**9+7;F=[1];a,b=sorted(map(int,input().split()))
for i in range(1,10**6):F.append(F[-1]*i%M)
print(0 if b-a>1 else F[a]*F[b]*(1 if b-a else 2)%M)

Submission status: Accepted

Solution code:
import sys
import math

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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: Accepted

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

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

ans = 0L

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

print ans

Submission status: Accepted

Solution code:
import math
mode=1000000007


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

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

Submission status: Accepted

Solution code:
#Python


N = map(int, raw_input().split())
inu=N[0]
saru=N[1]
#print N


def kai(x):
	sum=1
	while x!=1:
		sum=sum*x
		if sum > 1000000007:
			sum = sum%1000000007
		x-=1
	return sum



if inu == saru:
	print kai(inu)*kai(saru)*2%1000000007
elif abs(inu-saru)==1:
	print kai(inu)*kai(saru)%1000000007
else:
	print 0

Submission status: Accepted

Solution code:
import math
mod = 10**9 + 7

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

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

Submission status: Accepted

Solution code:
import sys

# sys.stdin = open('c1.in')

M = 10 ** 9 + 7


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


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


def read_int():
    return int(input())


def read_str():
    return input()


def solve():
    N = 10 ** 5 + 1
    fac = [0] * N
    fac[0] = 1
    for i in range(1, N):
        fac[i] = i * fac[i - 1] % M

    n, m = read_int_list()
    if n == m:
        return 2 * fac[n] * fac[m] % M
    if abs(n - m) == 1:
        return fac[n] * fac[m] % M
    else:
        return 0


def main():
    res = solve()
    print(res)


main()

Submission status: Accepted

Solution code:
"""
def factorial(x):
    if x == 0:
        return 1
    return x * factorial(x - 1)

RuntimeError: maximum recursion depth exceeded
"""

from math import factorial
n, m = map(int, input().split())

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

diff = abs(n - m)

if diff > 1:
    print(0)
    exit()

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

Submission status: Accepted

Solution code:
import math

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

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

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

if a - b == 1:
    print(math.factorial(a) * math.factorial(b) % (10 ** 9 + 7))
else:
    print(math.factorial(a) * math.factorial(b) * 2 % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
"""
def factorial(x):
    if x == 0:
        return 1
    return x * fact(x - 1)
"""
from math import *
n, m = map(int, input().split())

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

print(ans)

Submission status: Accepted

Solution code:
import math
N=input()
N,M=N.split()
N=int(N)
M=int(M)
Nk=1
Mk=1

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


for i in range(1,N+1):
    Nk=Nk*i%(1000000000+7)
for i in range(1,M+1):
    Mk=Mk*i%(1000000000+7)

if N!=M:
    print(int((Nk*Mk)%(1000000000+7)))
else:
    res=Nk*Mk*2
    print(int(res)%(1000000000+7))


quit()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1000000)

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

def factorial(a):
    if a == 0:
        return 1
    else:
        return (a * factorial(a - 1)) % 1000000007

if n < m:
    n, m = m, n

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

elif n - m == 1:
    z = ((factorial(n) % 1000000007) * (factorial(m) % 1000000007)) % 1000000007
    print(z)

else:
    z = 2 * ((factorial(n) % 1000000007) * (factorial(m) % 1000000007)) % 1000000007
    print(z)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math


def main():
    N, M = list(map(int, input().strip().split()))
    diff = abs(N - M)
    if diff > 1:
        print(0)
        return

    n_fact = math.factorial(N)
    m_fact = math.factorial(M)
    a = n_fact * m_fact
    if diff == 0:
        a = a * 2

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

	elif N - M == 1:
		n = 1
		for i in range(M):
			n = (n * (i + 1)) % 1000000007
		n = (n ** 2) % 1000000007
		n = (n * N) % 1000000007
		print(n)
	else:
		n = 1
		for i in range(N):
			n = (n * (i + 1)) % 1000000007
		n = (n ** 2) % 1000000007
		n = (n * M) % 1000000007
		print(n)

Submission status: Accepted

Solution code:
import math

dog_count, monkey_count = map(int, input().rstrip().split())

pattern_count = 0

if dog_count == monkey_count:
    pattern_count = math.factorial(dog_count) * math.factorial(monkey_count) * 2
elif math.fabs(dog_count - monkey_count) == 1:
    pattern_count = math.factorial(dog_count) * math.factorial(monkey_count) 

if pattern_count >= 10**9 + 7:
    pattern_count = pattern_count % (10**9 + 7)

print(pattern_count)

Submission status: Accepted

Solution code:
import math
        
if(__name__=="__main__"):
    N, M=[int(i) for i in input().split()]
    
    n = 0
    if(max(N,M) - min(N,M) > 1):
        print(n)
    else:
        if(max(N,M) - min(N,M) == 0):
            n = pow(math.factorial(N),2) * 2
            #print(n)
        else:
            min_fact = math.factorial(min(N,M))
            n = min_fact*max(N,M) * min_fact
            #print(n)
        
        n = n % (pow(10,9) + 7)
        print(n)

Submission status: Accepted

Solution code:
import math
def ini(): return int(input())
def inli(): return list(map(int, input().split()))
def inf(): return float(input())
def inlf(): return list(map(float, input().split()))
def inl(): return list(input())
def pli(): return "".join(list(map(str, ans)))

a = inli()
n=a[0]
m=a[1]
if n-m ==1 or m-n==1:
    if n < m:
        tmp =math.factorial(n)
        print((tmp * tmp * m)%1000000007)
    else:
       tmp = math.factorial(m)
       print((tmp * tmp *n)%1000000007)
elif n==m :
    tmp =math.factorial(m)
    print((tmp * tmp *2)%1000000007)

else :
    print(0)

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
import math

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

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

if N == M:
    p1 = math.factorial(N)
    p2 = math.factorial(M)
    print((p1 * p2 * 2) % (10 ** 9 + 7))
    exit()
else:
    p1 = math.factorial(N)
    p2 = math.factorial(M)
    print((p1 * p2) % (10 ** 9 + 7))
    exit()

Submission status: Accepted

Solution code:
a, b = map(int, input().split())
an = 1
bn = 1

if a - b >= 2 or b - a >= 2 :
    print (0)
elif a == b :
    for i in range(a) :
        an = (an * (i + 1)) % 1000000007
        bn = (bn * (i + 1)) % 1000000007
    print ((2 * an * bn)% 1000000007)
else :
    if b > a :
      a, b = b, a 
    for i in range(a) :
        an = (an * (i + 1)) % 1000000007
    for i in range(b) :
        bn = (bn * (i + 1)) % 1000000007
    print ((an * bn)% 1000000007)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

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


if n == m:
    a = n
    for i in range(2,n):
        a = (a * i) % 1000000007
    print((a * a * 2) % 1000000007)
elif n < m:
    a = n
    for i in range(2,n):
        a = (a * i) % 1000000007
    print((a * a * m) % 1000000007)
else:
    a = m
    for i in range(2,m):
        a = (a * i) % 1000000007
    print((a * a * n) % 1000000007)

Submission status: Accepted

Solution code:
import math

mozir=input()
hyo=mozir.split(' ')

dogs=int(hyo[0])
monks=int(hyo[1])


hou=10**9+7

if dogs == monks:
    # d_0 m_0 d_1 m_1 ...
    tmp_1=1
    for idx in range(1,dogs+1,1):
        tmp_1=(tmp_1*idx) % hou
    for idx in range(1,monks+1,1):
        tmp_1=(tmp_1*idx) % hou

    # m_0 d_0 m_1 d_1 ...
    tmp_2=1
    for idx in range(1,monks+1,1):
        tmp_2=(tmp_2*idx) % hou
    for idx in range(1,dogs+1,1):
        tmp_2=(tmp_2*idx) % hou
    ans=(tmp_1+tmp_2)%hou
    
elif dogs + 1 == monks:
    # d_0 m_0 d_1 ... m_(n-1) d_n
    ans=1
    for idx in range(1,dogs+1,1):
        ans=(ans*idx) % hou
    for idx in range(1,monks+1,1):
        ans=(ans*idx) % hou
    
elif monks + 1 == dogs:
    # m_0 d_0 m_1 ... d_(n-1) m_n
    ans=1
    for idx in range(1,monks+1,1):
        ans=(ans*idx) % hou
    for idx in range(1,dogs+1,1):
        ans=(ans*idx) % hou

else:
    ans=0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/python

import sys

_BASE = 10**9 + 7

def factorial(N):
    total = 1
    for i in xrange(1, N + 1):
        total *= i
        total %= _BASE
    return total % _BASE

def main(argv):
    for line in sys.stdin:
        N, M = map(lambda x: int(x), line.split(" ", 2))

        arrangement = 0
        if M == N:
            arrangement = factorial(M)**2 * 2
        elif abs(M - N) == 1:
            minimum = min(M, N)
            arrangement = factorial(minimum)**2 * (minimum + 1)

        print(arrangement % _BASE)

if __name__ == "__main__":
    main(sys.argv)

Submission status: Accepted

Solution code:
def kaijou(num):
  ac = 1
  for i in range(num):
    ac = ((i + 1) * ac) % (10 ** 9 + 7)
  return ac
[n,m] = list(map(int,input().split()))
x = abs(n - m)
ans = 0
if (x >= 2):
  ans = 0
elif (x == 1):
  ans = (kaijou(m) * kaijou(n)) % (10 ** 9 + 7)
elif (x == 0):
  ans = (kaijou(m) * kaijou(n) * 2) % (10 ** 9 + 7)
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math

N, M = [int(x) for x in input().split()]
bignum= int(1e9 + 7)

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

elif abs(M-N) == 1:
    orders = math.factorial(M) * math.factorial(N)
    ans = int(orders % (bignum))
    print(ans)
    
else:
    orders = math.factorial(M) * math.factorial(N) * 2
    ans = int(orders % (bignum))
    print(ans)

Submission status: Accepted

Solution code:
MOD = 10**9+7

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

N,M = map(int, raw_input().split())
A = max(N,M)
B = min(N,M)

def solve():
    if A-B > 1: return 0
    factorical = [1]
    for n in range (1,A+1):
        f = mul(factorical[n-1], n)
        factorical.append(f)
    if A == B:
        return mul(2, mul(factorical[A], factorical[B]))
    else:
        return mul(factorical[A], factorical[B])

print solve()

Submission status: Accepted

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

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

    h = max(N, M)
    l = min(N, M)
    ans = 1
    for i in range(l):
        ans *= h - i
        ans *= l - i
        ans = ans % 1000000007

    if h == l:
        ans = ans * 2 % 1000000007
    print(ans)

main()

Submission status: Accepted

Solution code:
#coding: utf-8
import sys

memo = {1:1}
def factorial(n):
    for i in range(len(memo)+1,n+1):
        if n not in memo:
            memo[i] = memo[i-1] * i % int(1e9+7)
    return memo[n]

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

Submission status: Accepted

Solution code:
from math import factorial
input_ = input().split(' ')
M = int(input_[0])
N = int(input_[1])
P = 10**9 + 7
if M == N-1:
    print((factorial(N) * factorial(N-1)) % P)
elif M == N:
    print((factorial(N) * 2 * factorial(N)) % P)
elif M == N+1:
    print((factorial(N) * factorial(N+1)) % P)
else:
    print(0)

Submission status: Accepted

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

import math

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

if abs(N - M) >= 2:
    print(0)
    
else:
    
    A = math.factorial(N)
    B = math.factorial(M)

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

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
import math
if math.fabs(n-m)>=2:
    print(0)
elif math.fabs(n-m)==1:
    x=math.factorial(n)*math.factorial(m)
    y=x%1000000007
    print(y)
else:
    a=math.factorial(m)
    b=a*a
    c=2*b
    d=c%1000000007
    print(d)

Submission status: Accepted

Solution code:
import math

N, M = list(map(int, input().split()))
cons = (10**9+7)
if N == M:
    s = math.factorial(N)
    print((s**2*2)%cons)

elif abs(N-M) == 1:
    N = min(N, M)
    s = math.factorial(N)
    print((s*(s*(N+1))%cons))
else:
    print(0)

Submission status: Accepted

Solution code:
import math

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

if abs(n - m) > 1:
    print(0)
else:
    t = math.factorial(n)
    s = math.factorial(m)

    if abs(n - m) == 1:
        print(t * s % (10 ** 9 + 7))
    else :
        print(t * s * 2 % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
import math

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

if n > m:
    m, n = n, m

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

Submission status: Accepted

Solution code:
n,m = [int(x) for x in input().split()]
d = 10**9 + 7

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

if n-m == 1:
	b = kaijou(m)
	ans = (b*b*n)%d
elif n-m == -1:
	b = kaijou(n)
	ans = (b*b*m)%d
elif n==m:
	b = kaijou(n)
	ans = (b*b*2)%d
else:
	ans = 0

print(ans)

Submission status: Accepted

Solution code:
from math import *
a,b = map(int, input().split())
if abs(a-b) > 1 :
    print(0)
else  :
    print((1 if abs(a-b) else 2)*factorial(a)*factorial(b)%1000000007)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
import math
N,M = [int(i) for i in input().split(" ")]
S = 10**9+7
kn = math.factorial(N) % S
if N == M :
    print((kn*kn*2)%S)
elif N-M == 1 or M-N==1 :
    km = (math.factorial(M) % S)
    print((kn*km)%S)
else:
    print(0)

Submission status: Accepted

Solution code:
import numpy as np

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

mod = 10**9+7

answer = 1

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

if (n - m)** 2 > 1:
    answer = 0
else:
    for i in range(n):
        answer = mul(answer,i+1)

    for j in range(m):
        answer = mul(answer,j+1)

    if n == m:
        answer = mul(answer,2)

print(answer)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def main():
  N,M = map(int,input().split())
  MOD = 10**9 + 7
  if abs(N-M) > 1:
    print(0)
    return
  if N == M:
    ret = 2
    for i in range(2,N+1):
      ret = (ret*i*i)%MOD
  else:
    ret = 1
    M,N = min(M,N),max(M,N)
    for i in range(2,M+1):
      ret = (ret*i*i)%MOD
    for i in range(M+1,N+1):
      ret = (ret*i)%MOD
  print(ret)

main()

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:
import math

if __name__ == "__main__":
    n, m = map(int, input().split())
    if not abs(n - m) <= 1:
        print(0)
    elif abs(n - m) == 1:
        print(math.factorial(n) * math.factorial(m) % (10**9+7))
    else:
        print(math.factorial(n) * math.factorial(m) * 2 % (10**9+7))

Submission status: Accepted

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

def factorial(x):
    global d
    ret = 1
    while True:
        ret *= x
        ret %= d
        if x == 1:
            break
        x -= 1
    return ret

if abs(n-m) > 1:
    print(0)
else:
    if abs(n-m) == 1:
        factor = 1
    else:
        factor = 2
    ans = (factor * factorial(n) * factorial(m))%d
    print(ans)

Submission status: Accepted

Solution code:
import math

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

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

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


if a - b == 1:
    print(math.factorial(a) * math.factorial(b) % (10 ** 9 + 7))
else:
    print(math.factorial(a) * math.factorial(b) * 2 % (10 ** 9 + 7))

Submission status: Accepted

Solution code:

import math
N,M = input().split(' ')
N = int(N)
M = int(M)
if abs(N-M) >= 2:
    print(0)
elif N == M:
    kai = math.factorial(N)
    print(kai*kai*2 % 1000000007)
else:
    L = max(N, M)
    kai = math.factorial(L-1)
    print(kai*kai*L % 1000000007)

Submission status: Accepted

Solution code:
def f():
  z =list(map(int,input().split(" ")))
  n=min(z)
  m=max(z)
  p=1000000000+7
  if m-n >1:
    print("0")
    return 0
  cnt= 1
  for i in range(1,1+m):
    cnt =( cnt*i)%p
  for i in range(1,1+n):
    cnt =(cnt*i)%p
  if m==n:
    cnt = (cnt*2)%p
  print(cnt)

f()

Submission status: Accepted

Solution code:
def f(n):
	s = 1
	for i in range(1, n + 1):
		s = (s * i) % int(10e8 + 7)
	return s

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

if a == b:
	print int((2 * f(a) * f(a)) % int(10e8 + 7))
elif (a - b == 1) or (b - a == 1):
	print int((f(a) * f(b)) % int(10e8 + 7))
else:
	print 0

Submission status: Accepted

Solution code:
import math
def c():
    N, M = map(int, input().split())
    f = math.factorial
    if abs(N-M) > 1:
        print(0)
    elif N > M or M > N:
        print( f(N)*f(M) % (10**9 + 7) )
    elif N == M:
        print( (f(N)*f(M))*2 % (10**9 + 7))

c()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

if __name__ == '__main__':
    N, M = list(map(int, input().split()))

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

Submission status: Accepted

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

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


def fact2(a, b):
    return (fact(a) * fact(b)) %M


if n == m:
    print(((fact(n)**2)*2) % M)
elif abs(n - m) == 1:
    print(fact2(n, m))
else:
    print(0)

Submission status: Accepted

Solution code:
from math import factorial
mod = 1000000007
N, M = map(int, input().split())
if M-N>1 or M-N<-1:
    print(0)
elif M == N:
    print(2*factorial(M)**2 % mod)
else:
    print(factorial(M)*factorial(N) % mod)

Submission status: Accepted

Solution code:
N ,M = map(int,input().split())
maxx = max(N,M)
minn = min(N,M)
if (maxx-minn)>1:
    print(0)
else:
    import math
    if maxx-minn == 1:
        n = 1
    else:
        n = 2
    print(n*math.factorial(N)*math.factorial(M)%(10**9+7))

Submission status: Accepted

Solution code:
N, M = [int(x) for x in input().split()]
ans = 0
permN = 1
permM = 1

for i in range(N):
	permN = permN * (i + 1) % (10 ** 9 + 7)
for i in range(M):
	permM = permM * (i + 1) % (10 ** 9 + 7)

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function
import sys

x = 10 ** 9 + 7


def fact(n):
    r = 1
    while n > 0:
        r = (r * n) % x
        n -= 1
    return r


def main():
    n, m = map(int, sys.stdin.readline().strip().split(" "))
    if abs(n - m) > 1:
        print(0)
        return
    elif abs(n - m) == 0:
        print((2 * fact(n) * fact(m)) % x)
    else:
        print((fact(n) * fact(m)) % x)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

import math
if n == m:
    dog = math.factorial(n)
    mon = math.factorial(m)
    ans = dog * mon * 2
elif n == m + 1 or n == m - 1:
    dog = math.factorial(n)
    mon = math.factorial(m)
    ans = dog * mon
else:
    ans = 0

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