Problem p02468 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

class Inverse {
private:
  long long mod;
  vector<long long> inv;

public:
  Inverse() {}

  Inverse(long long mod, long long n = 10000000) : mod(mod) {
    inv = vector<long long>(n, 1);
    for (int i = 2; i < n; ++i)
      inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  }

  long long operator()(long long a) const {
    if (a < (int)inv.size())
      return inv[a];
    long long b = mod, x = 1, y = 0;
    while (b) {
      long long t = a / b;
      swap(a -= t * b, b);
      swap(x -= t * y, y);
    }
    return (x %= mod) < 0 ? x + mod : x;
  }
};

class Mint {
private:
  static long long mod;
  static Inverse inverse;
  long long val;

public:
  Mint() {}

  Mint(const long long &val) {
    this->val = val % mod;
    if (this->val < 0)
      this->val += mod;
  }

  static void setMod(const long long &m) {
    mod = m;
    inverse = Inverse(m);
  }

  Mint operator+=(const Mint &m) {
    val += m.val;
    if (val >= mod)
      val -= mod;
    return *this;
  }

  Mint operator-=(const Mint &m) {
    val -= m.val;
    if (val < 0)
      val += mod;
    return *this;
  }

  Mint operator*=(const Mint &m) {
    val *= m.val;
    val %= mod;
    return *this;
  }

  Mint operator/=(const Mint &m) {
    val *= inverse(m.val);
    val %= mod;
    return *this;
  }

  Mint operator+(const Mint &m) const {
    Mint res = *this;
    res += m;
    return res;
  }

  Mint operator-(const Mint &m) const {
    Mint res = *this;
    res -= m;
    return res;
  }

  Mint operator*(const Mint &m) const {
    Mint res = *this;
    res *= m;
    return res;
  }

  Mint operator/(const Mint &m) const {
    Mint res = *this;
    res /= m;
    return res;
  }

  Mint operator++() { return val += 1; }

  operator long long() { return val; }
};

Mint pow(const Mint &m, long long n) {
  if (n == 0) {
    return 1;
  } else if (n < 0) {
    return pow(m, -n);
  }
  Mint mm = pow(m, n / 2);
  mm *= mm;
  if (n % 2)
    mm *= m;
  return mm;
}

long long Mint::mod;
Inverse Mint::inverse;

ostream &operator<<(ostream &os, Mint a) {
  os << (long long)a;
  return os;
}

istream &operator>>(istream &is, Mint &a) {
  long long n;
  is >> n;
  a = Mint(n);
  return is;
}

int main() {
  Mint::setMod(1000000007);
  Mint m;
  int n;
  cin >> m >> n;
  cout << pow(m, n) << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

class Inverse {
private:
  long long mod;
  vector<long long> inv;

public:
  Inverse() {}

  Inverse(long long mod, long long n = 1000000) : mod(mod) {
    inv = vector<long long>(n, 1);
    for (int i = 2; i < n; ++i)
      inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  }

  long long operator()(long long a) const {
    if (a < (int)inv.size())
      return inv[a];
    long long b = mod, x = 1, y = 0;
    while (b) {
      long long t = a / b;
      swap(a -= t * b, b);
      swap(x -= t * y, y);
    }
    return (x %= mod) < 0 ? x + mod : x;
  }
};

class Mint {
private:
  static long long mod;
  static Inverse inverse;
  long long val;

public:
  Mint() {}

  Mint(const long long &val) {
    this->val = val % mod;
    if (this->val < 0)
      this->val += mod;
  }

  static void setMod(const long long &m) {
    mod = m;
    inverse = Inverse(m);
  }

  Mint operator+=(const Mint &m) {
    val += m.val;
    if (val >= mod)
      val -= mod;
    return *this;
  }

  Mint operator-=(const Mint &m) {
    val -= m.val;
    if (val < 0)
      val += mod;
    return *this;
  }

  Mint operator*=(const Mint &m) {
    val *= m.val;
    val %= mod;
    return *this;
  }

  Mint operator/=(const Mint &m) {
    val *= inverse(m.val);
    val %= mod;
    return *this;
  }

  Mint operator+(const Mint &m) const {
    Mint res = *this;
    res += m;
    return res;
  }

  Mint operator-(const Mint &m) const {
    Mint res = *this;
    res -= m;
    return res;
  }

  Mint operator*(const Mint &m) const {
    Mint res = *this;
    res *= m;
    return res;
  }

  Mint operator/(const Mint &m) const {
    Mint res = *this;
    res /= m;
    return res;
  }

  Mint operator++() { return val += 1; }

  operator long long() { return val; }
};

Mint pow(const Mint &m, long long n) {
  if (n == 0) {
    return 1;
  } else if (n < 0) {
    return pow(m, -n);
  }
  Mint mm = pow(m, n / 2);
  mm *= mm;
  if (n % 2)
    mm *= m;
  return mm;
}

long long Mint::mod;
Inverse Mint::inverse;

ostream &operator<<(ostream &os, Mint a) {
  os << (long long)a;
  return os;
}

istream &operator>>(istream &is, Mint &a) {
  long long n;
  is >> n;
  a = Mint(n);
  return is;
}

int main() {
  Mint::setMod(1000000007);
  Mint m;
  int n;
  cin >> m >> n;
  cout << pow(m, n) << endl;
}