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
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
mod = 10**9 + 7
n, m = map(int, input().split())
diff = max(n, m) - min(n, m)

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

inup = math.factorial(n)
sarup = math.factorial(m)
if diff == 0:
    val = inup * sarup * 2
    print(val%mod)
elif diff == 1:
    val = inup * sarup
    print(val%mod)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
##############################

import math


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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if abs(N-M) == 0:
    tmp = p(N)
    ans = tmp * tmp * 2 % MOD
elif abs(N-M) == 1:
    tmp = p(min(N,M))
    ans = tmp * tmp *max(N,M) % MOD
else:
    ans = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split());a=abs(N-M);p=[0,1,2][(a<1)+(a<2)];m=10**9+7
for i in range(1,N+1):p=p*i%m
for i in range(1,M+1):p=p*i%m
print(p)

Submission status: Accepted

Solution code:
from math import factorial

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

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from math import factorial

MOD = 1000000007  # type: int

def solve(N: int, M: int):
    if(N == M):
        print((2*factorial(N)*factorial(M))%MOD)
    elif(N == M-1 or M == N-1):
        print((factorial(N)*factorial(M))%MOD)
    else:
        print(0)

    return


# Generated by 1.1.5 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    solve(N, M)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**9)

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

cond = N == M or N+1 == M or N == M+1
if cond == False:
	print(0)
	sys.exit()

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

if N == M:
	h = factorial(N)
	print((((h*h)%1000000007)*2)%1000000007)
else:
	h_1 = factorial(N)
	h_2 = factorial(M)
	print((h_1*h_2) % 1000000007)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

M = [1]
mul = 1
for i in range(1, 10**5+5):
    mul = (mul * i) % mod
    M.append(mul)

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

Submission status: Accepted

Solution code:
from math import factorial

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

import math

mod = 10**9 + 7

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

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

n = math.factorial(N)
if N == M:
    m = n
    ans = (2 * n * m) % mod
else:
    if N < M:
        m = M * n
    else:
        m = n // N
    ans = (n * m) % mod

print(ans)

Submission status: Accepted

Solution code:
import math
n, m = [int(el) for el in input().split(' ')]
prime = 10**9 + 7

def f(x):
    return(math.factorial(x) % prime)

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

Submission status: Accepted

Solution code:
import sys
import copy
import  math
import bisect
from collections import *
from operator import itemgetter
"""
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
"""
stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

mod = 10 ** 9 + 7
def calc(num):
    ans = 1
    for i in range(1, num+1):
        ans *= i
        ans %= mod
    return ans

n, m = na()
if abs(n - m) >= 2:
    print(0)
else:
    inu = calc(n)
    saru = calc(m)
    if n == m:
        print(inu * saru * 2 % mod)
    else:
        print(inu * saru % mod)

Submission status: Accepted

Solution code:
mod = 10**9+7

def kaizyou(x):
    y = 1
    for i in range(2, x+1):
        y = (y*i)%mod
    return y


N , M = map(int, input().split())
if abs(N-M) >= 2:
    ans = 0
elif N == M:
    ans = 2 * kaizyou(N) ** 2
elif N > M:
    ans = N * kaizyou(M) ** 2
else:
    ans = M * kaizyou(N) ** 2
print(ans%mod)

Submission status: Accepted

Solution code:
import math

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

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

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

Submission status: Accepted

Solution code:
import math

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

dog = math.factorial(n)%(10**9+7)
monkey = math.factorial(m)%(10**9+7)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

import math

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

Submission status: Accepted

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

if B-A>1:
    print(0)

elif B-A==1:
    ANS=1
    
    for i in range(1,A+1):
        ANS=ANS*i%mod

    for i in range(1,B+1):
        ANS=ANS*i%mod

    print(ANS)

else:
    ANS=1
    
    for i in range(1,A+1):
        ANS=ANS*i%mod

    for i in range(1,B+1):
        ANS=ANS*i%mod

    print(ANS*2%mod)

Submission status: Accepted

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

Submission status: Accepted

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

mod = 10**9+7

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

tmp = 1

for i in range(n):
    tmp = tmp * (i+1) % mod
    
for i in range(m):
    tmp = tmp * (i+1) % mod
    
if n == m:
    print( tmp*2 % mod)
else:
    print(tmp)

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
a=0
if N-M==1:
    t=1
    for i in range(1,M+1):
        t=t*i%(10**9+7)
    a=t**2*N%(10**9+7)
elif M-N==1:
    t=1
    for i in range(1,N+1):
        t=t*i%(10**9+7)
    a=t**2*M%(10**9+7)
elif M==N:
    t=1
    for i in range(1,N+1):
        t=t*i%(10**9+7)
    a=2*t**2%(10**9+7)
print(a)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc065/tasks/arc076_a
# C - Reconciled?
import math

# (n+m) C (m)
# 犬同士と猿同士が隣り合わないようにする

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

# 個体は識別されてる
if abs(N-M) <= 1:
    dog_p = math.factorial(N)
    monky_p = math.factorial(M)
    work = ((dog_p % mod) * (monky_p % mod)) % mod

    if N != M:
        ans = work
    else:
        ans = ((work % mod) * (2 % mod)) % mod
else:
    # 全匹並べられない
    ans = 0

print(ans)

Submission status: Accepted

Solution code:
x,y=map(int,input().split())

def arrange(x,y):
    if x>y:
        x,y=y,x
    if y-x>1:
        return 0

    ans=1
    mod=10**9+7
    for i in range(1, x+1):
        ans*=i
        ans%=mod
    for i in range(1, y+1):
        ans*=i
        ans%=mod
    if x==y:
        ans*=2
        ans%=mod
    return ans

print(arrange(x,y))

Submission status: Accepted

Solution code:
import math

MOD = 10 ** 9 + 7

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import factorial


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

Submission status: Accepted

Solution code:
def main():
    N, M = map(int, input().split())
    div = pow(10, 9) + 7
    fN, fM = 1, 1
    for i in range(1, N+1): fN = (i*fN) % div
    for i in range(1, M+1): fM = (i*fM) % div

    if N == M:
        return 2*fN*fM % div
    elif N == M+1 or N+1 == M:
        return fN*fM % div
    else:
        return 0


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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
total=1
mod=1000000007
if N==M:
    for i in range(1,N+1):
        total=total*i*i
        total=total%mod
    print((total*2)%mod)

elif N+1==M or M+1==N:
    total*=max(M,N)
    for i in range(1,min(M,N)+1):
        total=total*i*i
        total=total%mod
    print(total%mod)
else:
    print(0)

Submission status: Accepted

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

MOD = 10**9 + 7
def fact(x):
    res = 1
    for i in range(2, x+1):
        res *= i
        res %= MOD
    return res

if abs(n - m) > 1:
    ans = 0
elif abs(n - m) == 1:
    ans = (fact(n) * fact(m)) % MOD
else:
    # n == m
    ans = (fact(n) * fact(m) * 2) % MOD

print(ans)

Submission status: Accepted

Solution code:
import math

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import bisect
import collections
import itertools

def getint(): return int(input())
def getints(): return list(map(int, input().split()))
def getint2d(rows): return [getints() for _ in range(rows)]
def getgrid(rows): return [input() for _ in range(rows)]
def array1d(n, value): return [value for _ in range(n)]
def array2d(n, m, value): return [array1d(m, value) for _ in range(n)]

n,m=getints()

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

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

res = 0
if n == m:
    res = factorial(n) * factorial(m) * 2 % mod
else:
    res = factorial(n) * factorial(m) % mod

print(res)

Submission status: Accepted

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

n_pattern = 1
m_pattern = 1

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

for m in range(1, M+1):
  m_pattern *= m
  m_pattern = m_pattern % (10 ** 9 + 7)
  
if N > M + 1 or N < M - 1:
  print(0)
elif N == M + 1 or M == N + 1:
  print(n_pattern * m_pattern % (10 ** 9 + 7))
elif N == M:
  print(n_pattern * m_pattern * 2 % (10 ** 9 + 7))

Submission status: Accepted

Solution code:
import bisect
import heapq
import itertools
import math
import operator
import os
import re
import string
import sys
from collections import Counter, deque, defaultdict
from copy import deepcopy
from decimal import Decimal
from fractions import gcd
from functools import lru_cache, reduce
from operator import itemgetter, mul, add, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N, M = list(map(int, sys.stdin.readline().split()))


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


if abs(N - M) >= 2:
    print(0)
    exit()
if abs(N - M) == 1:
    a = factorial(N) % MOD * factorial(M) % MOD
else:
    a = factorial(N) % MOD * factorial(M) % MOD * 2 % MOD

print(a)

Submission status: Accepted

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

Submission status: Accepted

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

x = 10**9+7

if abs(n-m) >= 2:
  print(0)
  exit()
  
import math
if n == m:
  print((2*math.factorial(n)*math.factorial(m))%x)
else:
  print((math.factorial(n)*math.factorial(m))%x)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

fact = [1 for _ in range(M + 100)]
for i in range(1, M + 100):
    fact[i] = (fact[i - 1] * i) % MOD

if N == M:
    ans = fact[N] * fact[N] % MOD * 2 % MOD
else:
    ans = fact[N] * fact[M] % MOD

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")

MOD = 1000000007  # type: int


def fact(a):
    ans = 1
    while a > 0:
        ans *= a
        ans %= MOD
        a -= 1
    return ans


def solve(N: int, M: int):
    if abs(N-M) > 1:
        print(0)
        return
    if N == M:
        print(2*pow(fact(N), 2) % MOD)
    else:
        print(max(N, M)*pow(fact(min(N, M)), 2) % MOD)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    solve(N, M)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import time

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

st = time.time()

a = math.factorial(N) % 1000000007
b = math.factorial(M) % 1000000007
"""
if N == M:
    for i in range(1,N+1):
        a *= i % 1000000007
    b = a

elif N - M == 1:
    for i in range(1,M+1):
        b *= i % 1000000007
    a = b * N 
elif M - N == 1:
    for i in range(1,N+1):
        a *= i % 1000000007
    b = a * M 
"""

if abs(N-M) == 1:
    ans = a * b %1000000007
elif N == M:
    ans = 2*a*b % 1000000007
else:
    ans = 0

print(ans)

ed = time.time() -st
#print(ed)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 2019/08/08
# 高速改良版

def mod(n):
    return n%(10**9+7)

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

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

Submission status: Accepted

Solution code:
# 2019/08/08

from math import factorial

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if abs(N - M) <= 1 and N == M:
    print(2*math.factorial(N)*math.factorial(M)%mod)

elif abs(N - M) <= 1 and N != M:
    print(math.factorial(N)*math.factorial(M)%mod)

else:
    print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, fractions, itertools

MOD = 1000000007  # type: int

f = [1]

def solve(N: int, M: int):
    if abs(N - M) == 1:
        print((f[N]*f[M]) % MOD)
    elif N == M:
        print(2 * (f[N]*f[M]) % MOD)
    else:
        print(0)
    return


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int

    for i in range(1, 200000):
        f.append(f[-1] * i % MOD)
    solve(N, M)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(ret)

Submission status: Accepted

Solution code:

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

def main():
    N, M = [int(x) for x in input().split()]
    if N > M:
        N, M = M, N

    if N == M - 1:
        print((fact(N) * fact(M)) % 1000000007)
        return
    elif N == M:
        print((fact(N) * fact(M) * 2) % 1000000007)
    else:
        print(0)
    
main()

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
if abs(N-M) > 1:
  print(0)
else:
  num = max(N, M)
  dp = [1] * (num+1)
  for i in range(2, num+1):
    dp[i] = (i * dp[i-1])%(10**9+7)
  if N == M:
    print((dp[N]*dp[M]*2)%(10**9+7))
  else:
    print((dp[N]*dp[M])%(10**9+7))

Submission status: Accepted

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

def fact(N):
    ans = 1
    for i in range(1,N+1):
        ans *= i
        if(ans>(10**9)+7):
            ans = ans%((10**9)+7)
    return ans
    
def main(N,M):
    if(int(abs(N-M))>=2):
        print(0)
    elif(N==M):
        print((2*(fact(N))**2)%((10**9)+7))
    elif(N-M==1):
        print((N*(fact(M))**2)%((10**9)+7))
    elif(M-N==1):
        print((M*(fact(N))**2)%((10**9)+7))    

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

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((2*(math.factorial(n)*math.factorial(m)))%(10**9+7))

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
mod = int(1E9+7)

if abs(N-M) > 1:
    ans = 0
elif N == M:
    n = 1
    for i in range(1, N+1):
        n = n*i % mod
    ans = 2 * n * n % mod
else:
    n = 1
    for i in range(1, N+1):
        n = n*i % mod
    m = 1
    for i in range(1, M+1):
        m = m*i % mod
    ans = n * m % mod

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input=sys.stdin.readline
n,m=map(int,input().split())
INF=10**9+7
if abs(n-m)>=2:
    print(0)
else:
    if abs(n-m)==1:
        ans=1
        for i in range(n):
            ans=(ans*(i+1))%INF
        for i in range(m):
            ans=(ans*(i+1))%INF
        print(ans)
    else:
        ans=2
        for i in range(n):
            ans=(ans*(i+1))%INF
        for i in range(m):
            ans=(ans*(i+1))%INF
        print(ans)

Submission status: Accepted

Solution code:
import math

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

Submission status: Accepted

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

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

Submission status: Accepted

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

div=10**9+7
n=1
m=1
diff=abs(N-M)
for i in range(1,N+1):
    n*=i
    n%=div
for i in range(1,M+1):
    m*=i
    m%=div

if diff>1:
    ans=0
elif diff==0:
    ans=(2*n*m)%div
elif diff==1:
    ans=(n*m)%div
print(ans)

Submission status: Accepted

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

div=10**9+7
n=1
m=1
diff=abs(N-M)
for i in range(1,N+1):
    n=n*i%div
for i in range(1,M+1):
    m=m*i%div

if diff>1:
    ans=0
elif diff==0:
    ans=(2*n*m)%div
elif diff==1:
    ans=(n*m)%div
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import *

n, m = map(int, input().split())
fac_n = factorial(n) % (10 ** 9 + 7)
fac_m = factorial(m) % (10 ** 9 + 7)

ans = fac_n * fac_m % (10 ** 9 + 7)

print(0 if abs(n - m) > 1 else ans if n != m else (ans * 2)  % (10 ** 9 + 7))

Submission status: Accepted

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