#include <bits/stdc++.h>
using namespace std;
template <int M>
struct static_mint {
  static_assert(0 < M, "Module must be positive");
  int val;
  static_mint() : val() {}
  static_mint(long long x) : val(x % M) {
    if (val < 0) val += M;
  }
  static_mint pow(long long n) const {
    static_mint ans = 1, x(*this);
    while (n) {
      if (n & 1) ans *= x;
      x *= x;
      n /= 2;
    }
    return ans;
  }
  static_mint inv() const { return pow(M - 2); }
  friend static_mint pow(const static_mint &m, long long n) { return m.pow(n); }
  friend static_mint inv(const static_mint &m) { return m.inv(); }
  static_mint operator+() const {
    static_mint m;
    m.val = val;
    return m;
  }
  static_mint operator-() const {
    static_mint m;
    m.val = M - val;
    return m;
  }
  static_mint &operator+=(const static_mint &m) {
    if ((val += m.val) >= M) val -= M;
    return *this;
  }
  static_mint &operator-=(const static_mint &m) {
    if ((val -= m.val) < 0) val += M;
    return *this;
  }
  static_mint &operator*=(const static_mint &m) {
    val = (long long)val * m.val % M;
    return *this;
  }
  static_mint &operator/=(const static_mint &m) {
    val = (long long)val * m.inv().val % M;
    return *this;
  }
  friend static_mint operator+(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) += rhs;
  }
  friend static_mint operator-(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) -= rhs;
  }
  friend static_mint operator*(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) *= rhs;
  }
  friend static_mint operator/(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) /= rhs;
  }
  friend bool operator==(const static_mint &lhs, const static_mint &rhs) {
    return lhs.val == rhs.val;
  }
  friend bool operator!=(const static_mint &lhs, const static_mint &rhs) {
    return lhs.val != rhs.val;
  }
  static_mint &operator++() { return *this += 1; }
  static_mint &operator--() { return *this -= 1; }
  static_mint operator++(int) {
    static_mint result(*this);
    *this += 1;
    return result;
  }
  static_mint operator--(int) {
    static_mint result(*this);
    *this -= 1;
    return result;
  }
  template <typename T>
  explicit operator T() const {
    return T(val);
  }
  friend std::ostream &operator<<(std::ostream &os, const static_mint &m) {
    return os << m.val;
  }
  friend std::istream &operator>>(std::istream &is, static_mint &m) {
    long long x;
    is >> x;
    m = x;
    return is;
  }
};
template <typename>
struct is_mint : public std::false_type {};
template <int M>
struct is_mint<static_mint<M>> : public std::true_type {};
struct union_find {
  int n, c;
  std::vector<int> p, r;
  union_find() : n(), c() {}
  union_find(int n) : n(n), c(n), p(n), r(n, 1) { iota(p.begin(), p.end(), 0); }
  int count() const noexcept { return c; }
  int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
  bool merge(int x, int y) {
    int l1 = find(x);
    int l2 = find(y);
    if (l1 == l2) return false;
    if (r[l1] >= r[l2]) {
      r[l1] += r[l2];
      p[l2] = l1;
    } else {
      p[l1] = l2;
      r[l2] += r[l1];
    }
    c -= 1;
    return true;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  using mint = static_mint<1000000007>;
  int p, k;
  cin >> p >> k;
  if (k == 0) {
    cout << mint(p).pow(p - 1) << '\n';
  } else if (k == 1) {
    cout << mint(p).pow(p) << '\n';
  } else {
    union_find uf(p);
    for (int x = 0; x < p; x++) {
      uf.merge((long long)k * x % p, x);
    }
    cout << mint(p).pow(uf.count() - 1) << '\n';
  }
  return 0;
}

### Prompt

Your challenge is to write a CPP solution to the following problem:

As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where p is an odd prime number, the functional equation states that 

<image>

for some function <image>. (This equation should hold for any integer x in the range 0 to p - 1, inclusive.)

It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 109 + 7.

Input

The input consists of two space-separated integers p and k (3 ≤ p ≤ 1 000 000, 0 ≤ k ≤ p - 1) on a single line. It is guaranteed that p is an odd prime number.

Output

Print a single integer, the number of distinct functions f modulo 109 + 7.

Examples

Input

3 2


Output

3


Input

5 4


Output

25

Note

In the first sample, p = 3 and k = 2. The following functions work: 

  1. f(0) = 0, f(1) = 1, f(2) = 2. 
  2. f(0) = 0, f(1) = 2, f(2) = 1. 
  3. f(0) = f(1) = f(2) = 0. 

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
template <int M>
struct static_mint {
  static_assert(0 < M, "Module must be positive");
  int val;
  static_mint() : val() {}
  static_mint(long long x) : val(x % M) {
    if (val < 0) val += M;
  }
  static_mint pow(long long n) const {
    static_mint ans = 1, x(*this);
    while (n) {
      if (n & 1) ans *= x;
      x *= x;
      n /= 2;
    }
    return ans;
  }
  static_mint inv() const { return pow(M - 2); }
  friend static_mint pow(const static_mint &m, long long n) { return m.pow(n); }
  friend static_mint inv(const static_mint &m) { return m.inv(); }
  static_mint operator+() const {
    static_mint m;
    m.val = val;
    return m;
  }
  static_mint operator-() const {
    static_mint m;
    m.val = M - val;
    return m;
  }
  static_mint &operator+=(const static_mint &m) {
    if ((val += m.val) >= M) val -= M;
    return *this;
  }
  static_mint &operator-=(const static_mint &m) {
    if ((val -= m.val) < 0) val += M;
    return *this;
  }
  static_mint &operator*=(const static_mint &m) {
    val = (long long)val * m.val % M;
    return *this;
  }
  static_mint &operator/=(const static_mint &m) {
    val = (long long)val * m.inv().val % M;
    return *this;
  }
  friend static_mint operator+(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) += rhs;
  }
  friend static_mint operator-(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) -= rhs;
  }
  friend static_mint operator*(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) *= rhs;
  }
  friend static_mint operator/(const static_mint &lhs, const static_mint &rhs) {
    return static_mint(lhs) /= rhs;
  }
  friend bool operator==(const static_mint &lhs, const static_mint &rhs) {
    return lhs.val == rhs.val;
  }
  friend bool operator!=(const static_mint &lhs, const static_mint &rhs) {
    return lhs.val != rhs.val;
  }
  static_mint &operator++() { return *this += 1; }
  static_mint &operator--() { return *this -= 1; }
  static_mint operator++(int) {
    static_mint result(*this);
    *this += 1;
    return result;
  }
  static_mint operator--(int) {
    static_mint result(*this);
    *this -= 1;
    return result;
  }
  template <typename T>
  explicit operator T() const {
    return T(val);
  }
  friend std::ostream &operator<<(std::ostream &os, const static_mint &m) {
    return os << m.val;
  }
  friend std::istream &operator>>(std::istream &is, static_mint &m) {
    long long x;
    is >> x;
    m = x;
    return is;
  }
};
template <typename>
struct is_mint : public std::false_type {};
template <int M>
struct is_mint<static_mint<M>> : public std::true_type {};
struct union_find {
  int n, c;
  std::vector<int> p, r;
  union_find() : n(), c() {}
  union_find(int n) : n(n), c(n), p(n), r(n, 1) { iota(p.begin(), p.end(), 0); }
  int count() const noexcept { return c; }
  int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
  bool merge(int x, int y) {
    int l1 = find(x);
    int l2 = find(y);
    if (l1 == l2) return false;
    if (r[l1] >= r[l2]) {
      r[l1] += r[l2];
      p[l2] = l1;
    } else {
      p[l1] = l2;
      r[l2] += r[l1];
    }
    c -= 1;
    return true;
  }
};
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  using mint = static_mint<1000000007>;
  int p, k;
  cin >> p >> k;
  if (k == 0) {
    cout << mint(p).pow(p - 1) << '\n';
  } else if (k == 1) {
    cout << mint(p).pow(p) << '\n';
  } else {
    union_find uf(p);
    for (int x = 0; x < p; x++) {
      uf.merge((long long)k * x % p, x);
    }
    cout << mint(p).pow(uf.count() - 1) << '\n';
  }
  return 0;
}

```