Problem:
Score: 900 points

Problem Statement

In 2937, DISCO creates a new universe called DISCOSMOS to celebrate its 1000-th anniversary.

DISCOSMOS can be described as an H \times W grid. Let (i, j) (1 \leq i \leq H, 1 \leq j \leq W) denote the square at the i-th row from the top and the j-th column from the left.

At time 0, one robot will be placed onto each square. Each robot is one of the following three types:

Type-H: Does not move at all.

Type-R: If a robot of this type is in (i, j) at time t, it will be in (i, j+1) at time t+1. If it is in (i, W) at time t, however, it will be instead in (i, 1) at time t+1. (The robots do not collide with each other.)

Type-D: If a robot of this type is in (i, j) at time t, it will be in (i+1, j) at time t+1. If it is in (H, j) at time t, however, it will be instead in (1, j) at time t+1.

There are 3^{H \times W} possible ways to place these robots. In how many of them will every square be occupied by one robot at times 0, T, 2T, 3T, 4T, and all subsequent multiples of T?

Since the count can be enormous, compute it modulo (10^9 + 7).

Constraints

1 \leq H \leq 10^9

1 \leq W \leq 10^9

1 \leq T \leq 10^9

H, W, T are all integers.

Input

Input is given from Standard Input in the following format:

H W T

Output

Print the number of ways to place the robots that satisfy the condition, modulo (10^9 + 7).

Sample Input 1

2 2 1

Sample Output 1

9

Shown below are some of the ways to place the robots that satisfy the condition, where ., >, and v stand for Type-H, Type-R, and Type-D, respectively:

>> .. vv
.. .. vv

Sample Input 2

869 120 1001

Sample Output 2

672919729

Solution in C (GCC 5.4.1) (judge verdict: Runtime Error)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define NUM 1000000007

/*
int koyaku(int a,int b){
  if(a>b){
    if(a%b==0) return b;
    return koyaku(b,a-b);}
  else{
    if(b%a==0) return a;
    return koyaku(a,b-a);}
    }
*/

int koyaku(int a,int b){
  if(b==0) return a;
  return koyaku(b,a%b);
}

/*
long int power(int a){ //2**a%NUM
  if(a<30) return (long int)pow(2,a);
  long int tmp=(long int)pow(2,30)%NUM;
  return ((int)pow(tmp,a/30)%NUM)*(int)pow(2,a%30);}
*/

long int power2(int a,int b){
  if(b==1) return (long int)a;
  if(b%2==0){
    long int tmp=power2(a,b/2)%NUM;
    return (tmp*tmp)%NUM;}
  else{
    return (power2(a,b-1)*a)%NUM;}
}
  

long int disco(int h,int w,int t){
  if(t==1){
    long int sum=0;
    sum+=power2(2,h);
    //printf("%ld\n",sum);
    sum+=power2(2,w);
    //printf("%d\n",NUM);
    sum+=power2(2,koyaku(h,w));
    //printf("%ld\n",sum);
    return (sum-3)%NUM;}

  else{
    long int koyaht=koyaku(h,t);
    long int koyawt=koyaku(w,t);
    return (long int)power2(disco(h/koyaht,w/koyawt,1),koyaht*koyawt)%NUM;}
}

int main(){
  int h,w,t;
  scanf("%d%d%d",&h,&w,&t);
  long int sum=disco(h,w,t);
  printf("%ld\n",sum);
  return 0;
}