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: Runtime Error

Solution code:
import math
n, m = map(int, input().split())
MOD = 1000000007
a, b = n, m
count_n = n
count_m =m
count = 0
if abs(n-m) == 1:
  count_n = math.factorial(n) / MOD
  count_m = math.factorial(m) / MOD
  count = count_m * count_n % MOD
elif abs(n-m) == 0:
  count_n = math.factorial(n) % MOD
  count_m = math.factorial(m) % MOD
  count = 2 * count_m * count_n % MOD
else:
  count = 0

print(count % MOD)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

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

if n==m-1 or n==m+1:
  print((math.factorial(m)*math.factorail(n))%(1e9+7))
elif n==m:
  print((2*(math.factorial(m)**2))%(1e9+7))
else:
  print(0)

Submission status: Runtime Error

Solution code:
import math

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

if n==m-1 or n==m+1:
  print((math.factorial(m)*math.factorail(n))%(1e9+7))
elif n==m:
  print((2*(math.factorail(m)**2))%(1e9+7))
else:
  print(0)

Submission status: Runtime Error

Solution code:
X,A,B = map(int, input().split())

if A + X < B:
    print("dengerous")
if A <= B <= A + X:
    print("safe")
if B < A:
    print("delicious")

Submission status: Runtime Error

Solution code:
A, B = map(int,input().split())
mod = 10**9+7
def factorical(n):
  ret = 1
  for i in range(1,n+1):
    ret *= i
    ret %= 
  return ret%mod

a = factorical(A)
b = factorical(B)
if(abs(A-B) >= 2):
  print(0)
elif(A == B):
  print(a*b*2%mod)
else:
  print(a*b%mod)

Submission status: Runtime Error

Solution code:
A, B = map(int,input().split())

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

a = factorical(A)
b = factorical(B)
if(abs(M-N)>= 2):
  print(0)
elif(A == B):
  print(a*b*2%(10**9+7))
else:
  print(a*b%(10**9+7))

Submission status: Runtime Error

Solution code:
MOD = 10**9 + 7
def Mod_Permutation(n, k, mod):
    ret = 1
    for i in range(n - k + 1, n + 1):
        ret = ret * i % mod
    return ret
N, M = map(int, input().split())
if abs(N - M) > 1:
    print(0)
elif N == M:
    print(int(Mod_Permutation(N, N, MOD) * (2 * Mod_Permutation(N, M, MOD) % MOD) % MOD))
else:
    if N < M:
        swap(N, M)
    print(int(Mod_Permutation(N, N, MOD) * Mod_Permutation(M, M, MOD) % MOD))

Submission status: Runtime Error

Solution code:
MOD = 10**9 + 7
def Mod_Permutation(n, k, mod):
    ret = 1
    for i in range(n - k + 1, n + 1):
        ret = ret * i % mod
    return ret
N, M = map(int, input().split())
print(Mod_Permutation(N, N, MOD) * P(N+1, M, MOD) % MOD)

Submission status: Runtime Error

Solution code:
def a(n):
    if n < 2:
        return 1
    return a(n-1) * n

n, m = map(int, input().split())
if abs(n-m) > 1:
    print(0)
elif abs(n-m) == 1:
    print(a(n) * a(m) % 1000000007)
else:
    print(2 * a(n) * a(m) % 1000000007)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import math
a,b = ma()
mod = 10**9 + 7
m = max(a,b)
n = min(a,b)
k=math.factorial(m)
if m!=n:
    print(((k**2)*m)%mod)
else:
    print(((k**2)*2)%mod)

Submission status: Runtime Error

Solution code:
N, M = map(int, input().split())
mod = 10 ** 9 + 7
K = max(N, M)
kaijo = [1] * (K + 1) 

for i in range(K):
    kaijo[i + 1] = ((i + 1) * kaijo[i]) % mod
    
if abs(N - M) > 1:
    print(0)
    
else:
    res = 1
    res *= kaijo[N] * kaijo[M]
    if N == M:
        res *= 2
    
print(res % mod)

Submission status: Runtime Error

Solution code:
#include <iostream>
using namespace std;
using ll = long long;
const int INF = 1e9;
#define put(n) cout<<(n)<<endl
#define rep(i,N) for(int(i)=0;(i)<(N);++(i))
#define rrep(i,N) for(int (i)=(N-1);(i)>=(0);--(i))
#define all(v) (v).begin() , (v).end()
#define rall(v) (v).rbegin() , (v).rend()
#define pb(n) push_back(n)

int Factorual(int N,int num){
    if(N == 0) return num;
    return Factorual(N-1,num*N);
}


int main(){
    int N,M;
    cin>>N>>M;

    if(N == M) cout << 2 * Factorual(N,1) * Factorual(M,1) << endl;
    else if(abs(N-M) == 1) cout << Factorual(N,1) * Factorual(M,1) << endl;
    else cout << 0 << endl;
      
}

Submission status: Runtime Error

Solution code:
n,m = map(int,input().split())
if n<m:
  n,m = m,n
if n == m:
  s = 1
  for in range(1,m+1):
    s = s*i%1000000007
  s = s*s*2%1000000007
elif n == m+1:
  s = 1
  for for in range(1,m+1):
    s = s*i%1000000007
  s = s*s*n%1000000007
else:
  s = 0
print(s)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 01:10:44 2018

@author: Yuki
"""
mod = 1000000000 + 7
def func(n):
    r = 1
    for i in range(1, n+1):
        r *= i
        r %= mod
    return r

N, M = map(int, input().1split())
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    A = max(N, M)
    res = func(A)
    print(int(res**2/A)%mod)
else:
    print(int(2*func(N)**2)%mod)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 01:10:44 2018

@author: Yuki
"""
import sys
sys.setrecursionlimit(100001)
def func(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n*func(n-1)

N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    A = max(N, M)
    res = func(A)
    print(int(res**2/A))
else:
    print(int(2*func(N)**2))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 14 01:10:44 2018

@author: Yuki
"""
def func(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n*func(n-1)

N, M = map(int, input().split())
if abs(N-M) > 1:
    print(0)
elif abs(N-M) == 1:
    A = max(N, M)
    res = func(A)
    print(int(res**2/A))
else:
    print(2*func(N))

Submission status: Runtime Error

Solution code:
Q = 10**9 + 7
'''
def factorial(n):
    if n == 0:
        return 1
    else:
        return (n * factorial(n-1))%Q
'''
N, M = map( int, input().split())
N, M = min(N, M), max(N, M)
if N == M or N+1 == M:
    NN = 1
    MM = 1
    for i in range(1,N+1):
        NN = (NN*i)%Q
    if N == M:
        ans = (2*NN*NN)%Q
elif N+1 == M:
    ans = (NN*NN*M)%Q
else:
    ans = 0
print(ans)

Submission status: Runtime Error

Solution code:
import math

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

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

Submission status: Runtime Error

Solution code:
import math

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
lights = [int(raw_input()) for i in range(int(raw_input()))]
lamps = [0]*len(lights)
lamps[0] = 1
lampco = len(set(lights))
for i in xrange(len(lamps)):
    lamps[int(lights[i])-1] += 1
    if lamps[1] == 1:
        print i
        exit()
print -1

Submission status: Runtime Error

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

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

if abs(N-M)>=2:
    print(0)
elif abs(N-M)==0:
    ans=fact(N)*4
elif abs(N-M)==1:
    ans=fact(N)*fact(M)
else:
    print(0)

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

print(ans)

Submission status: Runtime Error

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

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

if abs(N-M)>=2:
    print(0)
elif abs(N-M)==0:
    ans=fact(N)*4
elif abs(N-M)==1:
    ans=fact(N)*fact(M)
else:
    print(0)

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

print(ans)

Submission status: Runtime Error

Solution code:
import functools
import operator

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

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

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

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

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

main(N, M, sub)

Submission status: Runtime Error

Solution code:
import itertools

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

if sub > 1:
    # 解なし
    print("0")
else:
    a = [i for i in range(N)]
    b = [i for i in range(M)]
    per_a = len(list(itertools.permutations(a)))
    per_b = len(list(itertools.permutations(b)))

    if sub == 1:
        ans = per_a * per_b
        print(ans)
    else:
        ans = per_a * per_b * 2
        print(ans)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
if -2 < (n - m) < 2:
  ans = 1
  for i in range(n):
    ans *= n
    ans %= 10_0000_0007
  for i in range(m):
    ans *= n
    ans %= 10_0000_0007
  print(ans)
else:
  print(0)

Submission status: Runtime Error

Solution code:
n, m = map(int, input().split())
if -2 < (n -m) < 2:
  ans = 1
  for i in range(n):
    ans *= n
    ans %= 10_0000_0007
  for i in range(m):
    ans *= n
    ans %= 10_0000_0007
  print(ans)
else:
  print(0)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N, M = list(map(int, input().split()))
dp = [0]*(max(N, M)+1)
mod = 10000007

def factorical(n):
    if dp[n] != 0:
        rec = dp[n]
    elif n == 1:
        rec = 1
    else:
        rec = n*factorical(n-1)
    dp[n] = rec % mod
    return rec

factorical(max(N, M))

if abs(N-M) > 1: print(0)
elif N == M:
    dog = dp[N]
    monkey = dp[M]
    ans = 2*dog*monkey%mod


else:
    dog = dp[N]
    monkey = dp[M]
    ans = dog*monkey%mod

print(ans)

Submission status: Runtime Error

Solution code:
import math
n,m = map(int, input().split())
if abs(n-m) <= 1:
    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))
else:
    print(0)

Submission status: Runtime Error

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

def newfact(n):
    
    return ((n * newfact(n-1)) % M if n > 1 else 1)

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: Runtime Error

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

def newfact(n):
    return (n * newfact(n-1)) % M

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: Runtime Error

Solution code:
MOD = 1000000007

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


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

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

Submission status: Runtime Error

Solution code:
MOD = 1e+9+7

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


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

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

Submission status: Runtime Error

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

ans = long(0)

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

print ans % 1000000007L

Submission status: Runtime Error

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

import sys
sys.setrecursionlimit(1000)
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: Runtime Error

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

Submission status: Runtime Error

Solution code:
import math

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

diff = abs(n - m)

if diff > 1:
    print(0)
elif diff == 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: Runtime Error

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

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: Runtime Error

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

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

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

print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <cmath>

using namespace std;

int fact(int n){
    long int sum=1;
    long int X=(int)pow(10,9)+7;
    for(long int i=1;i<=n;i++){
        sum=(sum*i)%X;
    }
    return (int)sum;
}

int main(){
    long int N,M,a,b;

    cin >> N;
    cin >> M;

    if(N==M){
        a=fact(N);
        b=fact(M);
        cout << (2*a*b)%((int)pow(10,9)+7) << endl;
    }else if((N-M)==1){
        a=fact(M);
        a=fact(N);
        cout << (a*b)%((int)pow(10,9)+7) << endl;
    }else if((M-N)==1){
        a=fact(N);
        a=fact(M);
        cout << (a*b)%((int)pow(10,9)+7) << endl;
    }else{
        cout << "0" << endl;
    }

}

Submission status: Runtime Error

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-Mi) == 1:
    n = math.factorial(N)*math.factorial(M)
    n %= int(7+1e9)
    print(n)
else:
    print(0)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <cmath>

using namespace std;

int fact(int n){
    long int sum=1;
    long int X=(int)pow(10,9)+7;
    for(long int i=1;i<=n;i++){
        sum=(sum*i)%X;
    }
    return (int)sum;
}

int main(){
    long int N,M,a,b;

    cin >> N;
    cin >> M;

    if(N==M){
        a=fact(N);
        cout << (2*a*a)%((int)pow(10,9)+7) << endl;
    }else if((N-M)==1){
        a=fact(M);
        b=a*N;
        cout << (a*b)%((int)pow(10,9)+7) << endl;
    }else if((M-N)==1){
        a=fact(N);
        b=a*M;
        cout << (a*b)%((int)pow(10,9)+7) << endl;
    }else{
        cout << "0" << endl;
    }

}

Submission status: Runtime Error

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

Submission status: Runtime Error

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

def kai(x):
    if x == 0:
       return 1
    else:
       return x * kai(x-1)

if abs(n - m) > 1:
	print(str(0))
else:
	print(str(kai(n + m) / (kai(n) * kai(m)) + 2 % (pow(10,9) + 7)))

Submission status: Runtime Error

Solution code:
def kaijou2(num):
  return kaijou(num,1)
def kaijou(num,ac):
  if (num == 0):
    return ac
  else:
    return kaijou(num - 1,(ac * num) % (10 ** 9 + 7))

[n,m] = list(map(int,input().split()))
x = abs(n - m)
ans = 0
if (x >= 2):
  ans = 0
elif (x == 1):
  ans = (kaijou2(m) * kaijou2(n)) % (10 ** 9 + 7)
elif (x == 0):
  ans = (kaijou2(m) * kaijou2(n) * 2) % (10 ** 9 + 7)
print(ans)