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:
import math
PRIME_NUM = 1000000007
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
elif N == M:
    print(2 * (math.factorial(N) % PRIME_NUM) * (math.factorial(M) % PRIME_NUM) % PRIME_NUM)
else:
    print((math.factorial(N) % PRIME_NUM) * (math.factorial(M) % PRIME_NUM) % PRIME_NUM)

Submission status: Accepted

Solution code:
# coding=utf-8
import math

line = input().split(" ")
n = int(line[0])
m = int(line[1])

def perm(x):
    return math.factorial(x)

p = 10**9 + 7

f = 1
if n==m:
    f = 2

if abs(n-m)>1:
    print(0)
else:
    print(f*perm(n)*perm(m)%p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
n,m=map(int,input().split())
ans=1
for i in range(1,n+1):
    ans=ans*i%1000000007
for i in range(1,m+1):
    ans=ans*i%1000000007

if abs(n-m)>1:
    print(0)
elif n!=m:
    print(ans)
else:
    print(ans*2%1000000007)

Submission status: Accepted

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

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

min_num = min(N, M)
max_num = max(N, M)
diff = abs(N-M)
K = 10**9 + 7

if diff >= 2:
    print(0)
    exit()

ans = 0
if diff == 1:
    ans = 1
else:
    ans = 2

for i in range(N):
    ans = ((i+1)*ans) % K

for i in range(M):
    ans = ((i+1)*ans) % K

print(ans)

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import math
import sys
 
n, m = map(int, input().split())
 
if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0
 
print(math.factorial(n) * math.factorial(m) * k % 1000000007)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import array
def get_fact(n, mod):
    fact = array.array('l')
    fact.append(1)
    for i in range(n):
        fact.append((i + 1) * fact[-1] % mod)
    return fact

mod = 10 ** 9 + 7
n, m = map(int, input().split())
fact = get_fact(max(n, m), mod)
if n == m:
    result = (fact[n] * fact[m]) * 2
elif abs(n - m) == 1:
    result = fact[n] * fact[m]
else:
    result = 0
print(result % mod)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, factorial
from collections import deque
from bisect import bisect, bisect_left, bisect_right
INF = 10 ** 10
MOD = 10 ** 9 + 7

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

    ans = 2 if diff == 0 else 1
    for i in range(1, N + 1):
        ans = (ans * i) % MOD
    for i in range(1, M + 1):
        ans = (ans * i) % MOD
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#ARC076
import math

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

Submission status: Accepted

Solution code:
def reconciled():
	n, m = map(int, raw_input().split(" "))
	if abs(n - m) > 1:
		print 0
		return
	mod = 10 ** 9 + 7
	mod_n = 1
	mod_m = 1
	for i in reversed(range(1, n + 1)):
		mod_n = (mod_n * i) % mod
	for j in reversed(range(1, m + 1)):
		mod_m = (mod_m * j) % mod
	if n != m:
		print (mod_n * mod_m) % mod
	else:
		print (2 * mod_n * mod_m) % mod


if __name__ == "__main__":
	reconciled()

Submission status: Accepted

Solution code:

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

mod = 10 ** 9 + 7
if N > M:
    N, M = M, N

if M - N >= 2:
    print(0)
elif M - N == 1:
    prod = 1
    for i in range(1, N + 1):
        prod = (prod * i) % mod
    print((prod * prod * M) % mod)
else:
    prod = 1
    for i in range(1, N + 1):
        prod = (prod * i) % mod
    print((2 * prod * prod) % mod)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
# N, M = 100000, 100000
# N, M = 2, 3

mod = 10 ** 9 + 7
if abs(N - M) >= 2:
    print(0)
else:
    if N == M:
        prod = 1
        for i in range(2, N + 1):
            prod = (prod * i) % mod
        print((prod * prod * 2) % mod)
    else:
        if N < M:
            N, M = M, N
        prod = 1
        ans = 1
        for i in range(2, N + 1):
            prod = (prod * i) % mod
            if i == M:
                ans = (ans * prod) % mod
        print((ans * prod) % mod)

Submission status: Accepted

Solution code:
import math as mt

a,b=map(int,input().split())
if a==b:print(2*mt.factorial(a)**2%1000000007)
elif mt.fabs(a-b)==1:print(mt.factorial(a)*mt.factorial(b)%1000000007)
else :print(0)

Submission status: Accepted

Solution code:
import sys
import math
n, m = (int(i) for i in input().split())
l=abs(n-m)
div=int(10**9+7)

if l>1:
    print(0)
    sys.exit()

if l==1:
    ans=(math.factorial(n)*math.factorial(m))%div
    print(ans)
else:
    ans=(math.factorial(n)*math.factorial(m)*2)%div
    print(ans)

Submission status: Accepted

Solution code:
def fac(n, MOD):
    cur = 1
    for i in range(1, n+1):
        cur = (cur*i)%MOD
    return cur

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

Submission status: Accepted

Solution code:
#encoding: utf-8

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

    ret=1
    mod=10**9+7
    for i in range(1,N+1):
        ret*=i
        ret%=mod
    for i in range(1,M+1):
        ret*=i
        ret %= mod
    if(N==M):
        r=2
    else:
        r=1
    print((ret*r)%mod)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
mod=10**9+7
n,m=map(int,raw_input().split())
mul1=mul2=1
if abs(n-m)>1:
    print 0
else:
    for i in range(1,n+1):
        mul1*=i
        mul1%=mod
    for i in range(1,m+1):
        mul2*=i
        mul2%=mod
    if n==m:
        print (2*mul1*mul2)%mod
    else:
        print (mul1*mul2)%mod

Submission status: Accepted

Solution code:
MODULO = 10**9+7

def perm(n):
	p = 1
	for i in range(1,n+1):
		p = (p*i) % MODULO 
	return p

n, m = map(int, input().strip().split())
x = abs(n-m)
if (x > 1):
	print(0)
else:
	permn = perm(n)
	permm = perm(m)

	if x==0: 
		print((permn*permm*2)%MODULO)
	else: 
		print((permn*permm)%MODULO)

Submission status: Accepted

Solution code:
from math import *
M,N=input().split(' ')
M=int(M)
N=int(N)

res=1
if M==N:
    res=factorial(M)
    res=res*res*2
elif M-N==1:
    res=factorial(N)
    res*=res*M
elif M-N==-1:
    res=factorial(M)
    res*=res*N
else:
    res=0

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

Submission status: Accepted

Solution code:
answer = 0
mod = 10**9 + 7
a,b = (int(n) for n in input().split(" "))
if abs(a-b) <= 1:
    temp1 = 1
    for i in range(1,a+1):
        temp1 = (temp1 * i) % mod
    temp2 = 1
    for i in range(1,b+1):
        temp2 = (temp2 * i) % mod
    answer += (temp1 * temp2) % mod
    if (a + b) % 2 == 0:
        answer = (answer * 2) % mod
    print(answer)
else:
    print(0)

Submission status: Accepted

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

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

if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0

print(math.factorial(n) * math.factorial(m) * k % 1000000007)

Submission status: Accepted

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

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

if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0

print(math.factorial(n) * math.factorial(m) % 1000000007 * k % 1000000007)

Submission status: Accepted

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

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

if math.fabs(n - m) == 1:
    k = 1
elif n == m:
    k = 2
else:
    k = 0

print(math.factorial(n) % 1000000007 * math.factorial(m) % 1000000007 * k % 1000000007)

Submission status: Accepted

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

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

ans = 0

ans1 = 1
n1 = n
m1 = m
while n1 >= 1:
    ans1 *= n1 * m1
    ans1 %= d
    n1 -= 1
    m1 -= 1

if m1 > 1:
    ans1 = 0

ans2 = 1
n2 = n
m2 = m
while m2 >= 1:
    ans2 *= m2 * n2
    ans2 %= d
    n2 -= 1
    m2 -= 1

if n2 > 1:
    ans2 = 0


print((ans1 + ans2) % d)

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:
import sys

N, M = (int(i) for i in raw_input().split())
ans = 1                                                                                                                                                                                                                                                                     

if __name__ == '__main__':
    if N == (M + 1) or N == (M - 1):
        for n in range(1, N+1):
            ans *= n
            ans %= 1e9 + 7
        for m in range(1, M+1):
            ans *= m
            ans %= 1e9 + 7
    elif N == M:
        for n in range(1, N+1):
            ans *= n
            ans %= 1e9 + 7
        for m in range(1, M+1):
            ans *= m
            ans %= 1e9 + 7
        ans *= 2
        ans %= 1e9 + 7
    else:
        ans = 0

    print int(ans)

Submission status: Accepted

Solution code:
from math import factorial


def solve(n, m):
    d = abs(n - m)
    if d > 1:
        return 0

    DIV = int(1e9 + 7)
    if d == 1:
        l = min(n, m)
        f = factorial(l) % DIV
        return f ** 2 * (l + 1) % DIV
    f = factorial(n) % DIV
    return f ** 2 * 2 % DIV


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

Submission status: Accepted

Solution code:
buf=input().split()
N=int(buf[0])
M=int(buf[1])

thres=10**9+7

if abs(N-M)==1:
    N=min(N,M)
    retVal=1
    for i in range(1,N+1):
        retVal*=(i**2)
        if retVal>=thres:
            retVal=retVal%thres
    retVal*=(N+1)
    if retVal>=thres:
        retVal=retVal%thres
    print(retVal) 
elif N==M:
    retVal=2
    for i in range(1,N+1):
        retVal*=(i**2)
        if retVal>=thres:
            retVal=retVal%thres
    print(retVal)
else:
    print(0)

Submission status: Accepted

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

if abs(N-M) <= 1:
  cnt = 2 - abs(N-M)
  for i in range(1,N+1):
    cnt *= i
    cnt %= MOD

  for i in range(1,M+1):
    cnt *= i
    cnt %= MOD
else:
  cnt = 0

print(cnt)

Submission status: Accepted

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

mod=10**9+7

def kaijo(n):
    result=1
    for i in range(2,n+1):
        result*=i
        if result>mod:
            result=result%mod
    return result
    
def count(n,m):
    return kaijo(n)*kaijo(m)

if n-m==-1 or n-m==1:
    result=count(n,m)
    amari=result%mod
    print amari
elif n-m==0:
    result=count(n,m)
    result*=2
    amari=result%mod
    print amari
else:
    print 0

Submission status: Accepted

Solution code:
from functools import reduce

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

mod = 1000000007

def factorial(n):
    return reduce(lambda x, y: x*y%mod, range(1, n+1))

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

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
M=10**9+7
def w(i,r=1):
 while i:r=r*i%M;i-=1
 return r
print(max(0,(2-abs(n-m))*w(n)*w(m))%M)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
MOD = pow(10, 9)+7
N, M = map(int, raw_input().split())

def myfact(num):
	res = 1
	for i in range(num+1)[1:]:
		res *= i
		if res > MOD:
			res %= MOD
	return res
if abs(N - M) > 1:
	print 0
else:
	if N == M:

		res = ( myfact(N) * myfact(N) * 2 )%MOD
	else:
		res = ( myfact(N) * myfact(M) )%MOD
	print res

Submission status: Accepted

Solution code:
dog_ape = list(map(int,input().split(" ")))
dog_ape.sort()
cut     = (10**9)+7

def kai(n):
    ans = 1
    r   = 0
    for i in range(n):
        ans *= (i+1)**2
        if ans > cut:
            ans %= cut
    return ans

if dog_ape[0] == dog_ape[1]:
    ans = ( kai(dog_ape[0]) )*2
    ans %= cut
    print(ans)

elif dog_ape[1] - dog_ape[0] == 1:
    ans = kai(dog_ape[0])*(dog_ape[1])
    ans %= cut
    print(ans)

else:
    print(0)

Submission status: Accepted

Solution code:
stan = 10**9+7
def pow(x):
    start = 1
    for i in range(1,x+1 ):
        start = start * i % stan
    return start
n,m = [int(x) for x in input().split(' ')]
n, m = max([n,m]), min([n,m])
if n - m > 1:
    print(0)
    exit()
if n - m == 1:
    sum = pow(n) * pow(m)
    print(sum % stan)
elif n == m:
    sum = pow(n) * pow(m)
    print(2 * sum % stan)

Submission status: Accepted

Solution code:
import math
n, m = [int(i) for i in input().split()]
print(0 if abs(n-m) > 1 else (math.factorial(n) * math.factorial(m) * (2 if n == m else 1)) % int(1e9 + 7))

Submission status: Accepted

Solution code:
import math
max = 1000000007
def test(x):
  return x if x<max else (x % max)

def kai(m):
  if m == 1:
    return 1
  else:
    return test(kai(m-1) * m)

n,m = map(int,input().split())
ret = 0
if n == m:
  k = math.factorial(m)
  ret = test(2 * k * k)
elif n-m == 1:
  k = math.factorial(m)
  ret = test(k * k * n)
elif n-m == -1:
  k = math.factorial(n)
  ret = test(k * k * m)
print(ret)

Submission status: Accepted

Solution code:
import sys
import math

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

arr=[]
arr =(input().split())

n = int(arr[0])
m = int(arr[1])

modulus = 1000000007

if abs(n-m)>1 :
    print(0)
elif n==m:
    ans = (fact(n,modulus)*fact(m,modulus))%modulus
    ans = ans+ans
    if ans > modulus:
        ans-=modulus
    print(ans)
else:
    print( (fact(n,modulus)*fact(m,modulus))%modulus)

Submission status: Accepted

Solution code:
import math
import sys

a = sys.stdin.readline().split(' ')
n = int(a[0])
m = int(a[1])

modulator = 1000000007

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

Submission status: Accepted

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

if abs(N - M) > 1:
    print(0)
elif N == M:
    res = math.factorial(N) * math.factorial(M) * 2
    print(res % int(1e9 + 7))
else:
    res = math.factorial(N) * math.factorial(M)
    print(res % int(1e9 + 7))

Submission status: Accepted

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

import math

MAX = 1000000007

def ans(m,n):
    if ( abs(m - n) > 1 ):
        print(0);
        return 0

    if ( abs(m - n) == 1 ):
        print((math.factorial(m)*math.factorial(n))%MAX);
        return 0
    
    if ( abs(m - n) == 0 ):
        print((2*(math.factorial(m)*math.factorial(n)))%MAX);
        return 0

m,n = [int(i) for i in input().split()]
ans(m,n)

Submission status: Accepted

Solution code:
# coding: utf-8
import array, bisect, collections, copy, heapq, itertools, math, random, re, string, sys, time
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 20
MOD = 10 ** 9 + 7


def II(): return int(input())
def ILI(): return list(map(int, input().split()))
def IAI(LINE): return [ILI() for __ in range(LINE)]
def IDI(): return {key: value for key, value in ILI()}


def read():
    N, M = ILI()
    return N, M


def solve(N, M):
    if abs(N - M) >= 2:
        return 0
    elif abs(N - M) == 1:
        num_min = min(N, M)
        ans = ((math.factorial(num_min) ** 2) * (num_min + 1)) % MOD
        return ans
    elif abs(N - M) == 0:
        ans = (2 * (math.factorial(N) ** 2)) % MOD
        return ans


def main():
    params = read()
    print(solve(*params))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import numpy as np

def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline

def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, M] = [int(input_raw[0]), int(input_raw[1])]

    dog_ord_num = 1
    mon_ord_num = 1

    if abs(N - M) <= 1:
        for i in range(N):
            dog_ord_num *=  i + 1
            dog_ord_num = dog_ord_num % (int(1e+9) + 7)

        for i in range(M):
            mon_ord_num *= i + 1
            mon_ord_num = mon_ord_num % (int(1e+9) + 7)



        all_ord = (dog_ord_num * mon_ord_num ) % (int(1e+9) + 7)
        if (abs(N - M) == 0):
            all_ord *= 2
            all_ord %= (int(1e+9) + 7)

    else:
        all_ord = 0

    print int(all_ord)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def factorial(n):
    x = n
    r = 1
    while x != 1:
        r *= x
        r %= d
        x -= 1
    return r

d = (10**9) + 7
n, m = map(int, raw_input().split())
if not(n-1 <= m <= n+1):
    print 0
    exit()
if n == m:
    c = factorial(n)**2 * 2 % d
else:
    c = factorial(n) * factorial(m) % d
print c

Submission status: Accepted

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

mod = int(1e9) + 7

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

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

def solve(n, m):
  if n == m:
    return p(n) * p(m) * 2 % mod
  else:
    return p(n) * p(m) % mod

if n == m or n + 1 == m:
  print(solve(n, m))
else:
  print(0)

Submission status: Accepted

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

fact = [0 for _ in range(100000+1)]
fact[0] = 1
for i in xrange(1, 100000+1):
    fact[i] = (fact[i-1] * i) % (10**9 + 7)

ans = 0
if N == M:
    ans += fact[N] * fact[M]
    ans += fact[M] * fact[N]
if N == M - 1 or M == N - 1:
    ans += fact[N] * fact[M]

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

Submission status: Accepted

Solution code:

def Factorial(integer) :
    global M
    product = 1
    if integer == 0 : return 1

    for num in range(1 ,integer + 1):
        product *= num
        product%=M
    return product%M


d=sorted(list(map(int,input().split(" "))))
M=10**9+7

if abs(d[0]-d[1])>1:
    res=0
elif d[0]==d[1]:

    res=((Factorial(d[0])%M)**2)*2
else:
    res=Factorial(d[1])*Factorial(d[0])

print(res%(M))

Submission status: Accepted

Solution code:
import math

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

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

Submission status: Accepted

Solution code:
read = lambda: map(int, input().split())
mod = 10 ** 9 + 7
n, m = read()
if abs(n - m) > 1:
    print(0)
    exit()
N = max(n, m) + 1
fac = [0] * N
fac[0] = 1
for i in range(1, N):
    fac[i] = (fac[i - 1] * i) % mod
if n == m:
    ans = (2 * fac[n] * fac[m]) % mod
else:
    ans = fac[n] * fac[m] % mod
print(ans % mod)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# C
N, M = map(int, input().split())
L = 1000000007

res = 1

if (M - N) >= 2 or (N - M) >= 2:
    res = 0
else:
    for k in range(M):
        res *= (k+1)
        res = res % L
    for k in range(N):
        res *= (k+1)
        res = res % L
    if M == N:
        res *= 2
        res = res % L
print(res)

Submission status: Accepted

Solution code:
from math import factorial

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

def foo(N,M):
	if N == M+1 or N+1 == M:
		tm = factorial(min(N,M))
		return(tm*max(N,M)*tm)

	elif N==M:
		return((factorial(N)**2)*2)

	else:
		return(0)

print(foo(N,M)%(10**9+7))

Submission status: Accepted

Solution code:
import math
fact = math.factorial
n, m = [int(i) for i in input().split()]

def dog_monk(n,m):
    diff = abs(n-m)
    if diff > 1:
        return 0
    elif diff == 1:
        return fact(m) * fact(n)
    elif diff == 0:
        return 2 * fact(m) * fact(n)

print(dog_monk(n,m) % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
from math import factorial
n, m = [int(i) for i in input().split()]
const = 10**9 + 7
nf = factorial(n) % const
mf = factorial(m) % const
ret = (nf * mf) % const
if n - m == 1 or m - n == 1:
    ret = (nf * mf) % const
elif n == m:
    ret *= 2
    ret %= const
else:
    ret = 0
print(ret)

Submission status: Accepted

Solution code:
def ri(): return int(input())
def rli(): return list(map(int, input().split()))
def rls(): return list(input())
def pli(a): return "".join(list(map(str, a)))


N, M = rli()
ans = 1
mod = 1000000007
if(abs(N-M)>=2):
    ans = 0
else:
    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 = (ans*2) % mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys

def main():
    line = sys.stdin.readline()
    lst = line.split(' ')
    a = int(lst[0])
    b = int(lst[1])
    n = min(a,b)
    m = max(a,b)

    if (m == n+1):
    #n! * m!

        ans = 1
        for i in range(1,n+1):
            ans = (ans * i) % 1000000007

        for i in range(1,m+1):
            ans = (ans * i) % 1000000007

        print(ans)
    elif (m == n):
        ans = 2
        for i in range(1,n+1):
            ans = (ans * i) % 1000000007

        for i in range(1,m+1):
            ans = (ans * i) % 1000000007
        print(ans)
    else:
        print(0)
    return 0

if __name__ == '__main__':
    sys.exit(main())

Submission status: Accepted

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

N, M = [int(n) for n in input().split()]
if abs(N-M) == 0:
    result = factorial(N)**2 * 2
elif abs(N-M) == 1:
    result = factorial(N) * factorial(M)
else:
    result = 0
    
print(result % (10**9 + 7))

Submission status: Accepted

Solution code:
MOD = 1000000007
MAX = 100005
P = [0 for i in range(MAX)]
P[0] = 1
for i in range(MAX-1):
    P[i+1] = P[i] * (i + 1)
    P[i+1] %= MOD

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

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

Submission status: Accepted

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


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


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

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

maxv = max(N, M)
minv = min(N, M)

if maxv == minv:
    ans = f(maxv)
    ans *= f(minv)
    ans *= 2
    ans %= MOD
    print(ans)
    exit()

ans = f(maxv)
ans *= f(minv)
ans %= MOD
print(ans)

Submission status: Accepted

Solution code:
import math
p = 1000000007
if __name__ == '__main__':
    n,m = list(map(int,input().split()))
    if n==m:
        print(((math.factorial(n)%p)*(math.factorial(n)%p)*2)%p)
    elif abs(n-m)==1:
        print((math.factorial(m)%p)*(math.factorial(n)%p)%p)
    else:
        print(0)

Submission status: Accepted

Solution code:
temp=input().split()
N=int(temp[0])
M=int(temp[1])
ans=0
INF=1000000000

if abs(N-M)==0:
	ans=2
elif abs(N-M)==1:
	ans=1
else:
	ans=0

#を求める
for i in range(1,N+1):
	ans*=i
	ans%=INF+7
for i in range(1,M+1):
	ans*=i
	ans%=INF+7
print(ans)

Submission status: Accepted

Solution code:
import sys

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

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


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


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


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


def read_str():
    return input()


def solve(n, m):
    if abs(n - m) > 1:
        return 0
    res = f[m] * f[n]
    res %= M
    if m == n:
        res *= 2
        res %= M
    return res


f = [1] * (N + 1)
for i in range(1, N + 1):
    f[i] = i * f[i - 1]
    f[i] %= M

n, m = read_int_list()
res = solve(n, m)
print(res)

Submission status: Accepted

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

def fac(n, mod):
    a = 1
    for i in range(n, 0, -1):
        a = (a * i) % mod
    return a
    
def solve(N, M):
    mod = 10**9 + 7
    if N > M + 1 or M > N + 1:
        return 0
    if N == M:
        return (2 * fac(N, mod)**2) % mod
    else:
        return (fac(N, mod) * fac(M, mod)) % mod

print(solve(N, M))

Submission status: Accepted

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

import array
from bisect import *
from collections import *
import fractions
import heapq 
from itertools import *
import math
import re
import string

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

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

Submission status: Accepted

Solution code:
def init_fact(n):
    ret=[0]*(n+1)
    ret[0]=1
    for i in xrange(1,n+1):
        ret[i]=ret[i-1]*i%mod
    return ret

mod=10**9+7
n,m=map(int,raw_input().split())
if abs(n-m)>=2:
    print 0
    exit()
fact=init_fact(10**5+5)
if 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())
if abs(N-M)>1:
  print(0)
elif abs(N-M)==1:
  n=math.factorial(N)%(10**9+7)
  m=math.factorial(M)%(10**9+7)
  print((n*m)%(10**9+7))
else:
  n=math.factorial(N)%(10**9+7)
  m=math.factorial(M)%(10**9+7)
  print((2*n*m)%(10**9+7))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

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

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()


def main():
    N,M = sorted(LI())
    if abs(N-M) > 1:
        return 0
    nr = 1
    for i in range(2,N+1):
        nr *= i
        nr %= mod
    if N == M:
        nr = nr * nr
        nr %= mod
        nr *= 2
        nr %= mod
        r = nr
    else:
        mr = nr * M
        mr %= mod
        r = nr * mr
        r %= mod

    return r

print(main())

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
if m<n-1 or n+1<m:
    print(0)
    exit()
MOD = 10**9+7

ans = 1
for i in range(2, n+1):
    ans = (ans * i)%MOD
for i in range(2, m+1):
    ans = (ans * i)%MOD
if n == m:
    ans = (ans * 2)%MOD
print(ans)

Submission status: Accepted

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

MOD = 10**9 + 7
if 1 < abs(n - m):
    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, raw_input().split())
MOD = 10**9+7

F = [0 for _ in xrange(10**5+10)]
F[0] = 1
F[1] = 1
for i in xrange(2, 10**5+2):
    F[i] = F[i-1] * i % MOD

if abs(n - m) >= 2:
    print 0
elif abs(n - m) == 1:
    print F[n] * F[m] % MOD
else:
    print F[n] * F[m] * 2 % MOD

Submission status: Accepted

Solution code:

import sys
#sys.stdin=open("data.txt")
input=sys.stdin.readline

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

if n==m: cnt=2
elif abs(n-m)==1: cnt=1
else: cnt=0

# permute now
for i in range(1,n+1): cnt=(cnt*i)%(1000000007)
for i in range(1,m+1): cnt=(cnt*i)%(1000000007)

print(cnt)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
def C(n, r ,mod=10**9+7):
    if n-r<r: r=n-r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k]) %mod

    return result % mod
print(C(a+1,b))