1. 程式人生 > >Codeforces 894 B Ralph And His Magic Field

Codeforces 894 B Ralph And His Magic Field

con gic ans into dex style cond code pan

Discription

Ralph has a magic field which is divided into n?×?m blocks. That is to say, there are n rows and m columns on the field. Ralph can put an integer in each block. However, the magic field doesn‘t always work properly. It works only if the product of integers in each row and each column equals to k

, where k is either 1 or -1.

Now Ralph wants you to figure out the number of ways to put numbers in each block in such a way that the magic field works properly. Two ways are considered different if and only if there exists at least one block where the numbers in the first way and in the second way are different. You are asked to output the answer modulo1000000007?=?109

?+?7.

Note that there is no range of the numbers to put in the blocks, but we can prove that the answer is not infinity.

Input

The only line contains three integers n, m and k (1?≤?n,?m?≤?1018, k is either 1 or-1).

Output

Print a single number denoting the answer modulo 1000000007.

Example

Input
1 1 -1
Output
1
Input
1 3 1
Output
1
Input
3 3 -1
Output
16

Note

In the first example the only way is to put -1 into the only block.

In the second example the only way is to put 1 into every block.

/*
  1:當k==1的情況:
    這樣需要保證每一行和每一列的乘積都是1,也就是
    每一行和每一列的-1的個數都是偶數。
    
    我們可以填前n-1行的時候只需要保證行的限制,先
    不管列的限制。
    這樣的方案數是 2^((n-1)*(m-1))  (楊輝三角同一行
    的奇數項和=偶數項和)
    然後最後一行再填坑,對於每一列,如果前n-1行的乘積
    是-1,那麽填-1;否則填1。
    發現這樣的方案是唯一的,而且是一定可行的。
    證明可行的話,因為前n-1行的乘積是1,而這一行填完之後
    每一列的乘積也是1,所以除一下之後,這一行的乘積也是1了。
    
  2:當k==-1的情況:
    當n,m奇偶性不同的時候無解,因為如果行和列其中一個是奇數的
    時候,可以推出所有元素乘積是-1;而偶數則可以推出所有元素乘積是1,
    矛盾。
    
    當n,m奇偶性相同的時候,答案和1一樣,證明略。
    (也是前n-1行只需滿足單行限制的填數,最後一行填坑,
    仍可以證明方案唯一且可行) 
*/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define ll long long
#define ha 1000000007
using namespace std;
ll n,m;
int k;

inline ll ksm(ll x,ll y){
    ll an=1;
    for(;y;y>>=1,x=x*x%ha) if(y&1) an=an*x%ha;
    return an;
} 

int main(){
    scanf("%lld%lld%d",&n,&m,&k);
    if(k==-1&&((m^n)&1)) puts("0");
    else printf("%lld\n",ksm(ksm(2,m-1),n-1));
    return 0;
}

Codeforces 894 B Ralph And His Magic Field