Problem:
Problem D: Treasure Mountains
Problem
$N$ 個の山があり、 それぞれ $1 , 2 , \ldots ,N$ の番号が割り振られています。
ここには、たくさんのお宝が眠っていると噂されています。
    また、この地域には長さ $M$ の数列が言い伝えられており、その数列の $i (1 ≤ i ≤ M)$ 番目の数字は $d_i$ です。
山田君は $Q$ 個の巻物を入手しました。 $i (1 ≤ i ≤ Q)$ 番目の巻物には、3つの数字 $x_i, y_i, z_i$ が書かれています。
    巻物の3つの数字は、宝のある山の番号を探すヒントになるようです。
また、巻物には次のような情報も書かれていました。
宝のある山の番号を $t$ とする。 $t$ を $z$ 回暗号化したものが $x$ である。
1回目の暗号化では、 $d_y$ を鍵として使用する。
それ以降の暗号化では、前回使った鍵の番号を $p$ として、 $d_{p+1}$ を鍵として使用する。ただし、前回使った鍵が $d_{M}$ の場合のみ、 $d_{1}$ を次の鍵とする。
$t$ を鍵 $a$ で暗号化する、という操作は次のことを表す。
$t^a$ を $N+1$ で割った余りを新たな $t$ とする。
複数の巻物が同じ山の番号を指すこともあります。
計算が苦手な山田君の代わりに、それぞれの巻物が指している山の番号を答えてください。
なお、この制約において、それぞれの巻物が指す山の番号は一意に求まることが証明できます(
制約をよく確認することを推奨します
)。
Constraints
入力は以下の条件を満たす。
$4 ≤ N ≤ 9999990$
$N+1$ は素数である。
$1 ≤ d_i ≤ N - 1$
$d_i$ は $N$ と互いに素である。
$1 ≤ M, Q ≤ 50000$
$1 ≤ x_i ≤ N$
$1 ≤ y_i ≤ M$
$1 ≤ z_i ≤ 10^9$
入力は全て整数である。
Input
入力は以下の形式で標準入力から与えられる。
$N$ $M$ $Q$
$d_1$ $d_2$ $\ldots$ $d_M$
$x_1$ $y_1$ $z_1$
$\vdots$
$x_Q$ $y_Q$ $z_Q$
Output
$Q$ 行出力してください。 $i$ 行目には、 $i$ 番目の巻物に書かれている宝が存在する山の番号を答えてください。
また、末尾に改行を出力するのを忘れないようにしてください。
Sample Input 1
10 10 5
1 3 7 9 1 3 3 3 9 3
5 6 1
4 6 2
9 6 3
8 10 3
1 4 100
Sample Output 1
3
3
3
7
1
例えば、 $t = 3, y = 6$ として順に3回暗号化した場合のことを考えます。
1回暗号化すると、 $3^3 = 27$ であるから、
$27 \equiv 5 \mod 11$
となり、これは1つ目の巻物の情報 $(5, 6, 1)$ と一致します。
2回目の暗号化を施すと、 $5^3 = 125$ であるから、
$125 \equiv 4 \mod 11$
となり、これは2つ目の巻物の情報 $(4, 6, 2)$ と一致します。
3回目の暗号化も同様に、 $4^3 = 64$ であるから、
$64 \equiv 9 \mod 11$
となり、これは3つ目の巻物の情報 $(9, 6, 3)$ と一致します。
$t$ が $3$ 以外のとき、巻物の情報と一致することがないので、最初の3行は全て $3$ を出力すればよいです。
$y = 10$ の場合に3回暗号化するときは、 $d_{10}, d_1, d_2$ を順に鍵として使用します。
$t = 7$ とすると、 $7 \to 2 \to 2 \to 8$ と変化し、これは4つ目の巻物の情報 $(8, 10, 3)$ と一致します。
これ以外に条件を満たす $t$ はありません。よって、4行目には $7$ を出力します。
$t = 1$ のとき、何乗しても $1$ であるため、5つめの巻物の宝がある山は $1$ となります。
よって、5行目には $1$ を出力します。

Submitted solutions:

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long int ll;

ll mod_inv(ll a,ll p){
	ll b=p,x=1LL,y=0LL;
	while(b){
		ll t=a/b;
		a-=t*b; swap(a,b);
		x-=t*y; swap(x,y);
	}
	return (x%p+p)%p;
}

ll mod_pow(ll a,ll n,ll mod){
	ll res=1LL;
	while(n){
		if(n%2)res=res*a%mod;
		a=a*a%mod;
		n>>=1;
	}
	return res;
}

int main(){
	int n,m,q; cin >> n >> m >> q;
	vector<ll> d(m);
	for(int i=0;i<m;i++){
		cin >> d[i]; d[i]=mod_inv(d[i],n);
	}
	vector<ll> mul(2*m+1);
	mul[0]=1LL;
	for(int i=0;i<2*m;i++){
		mul[i+1]=mul[i]*d[i%m]%n;
	}
	while(q--){
		int x,y,z; cin >> x >> y >> z; y--;
		ll r=mod_pow(mul[m],z/m,n);
		r=r*mul[y+z%m]%n*mod_inv(mul[y],n)%n;
		cout << mod_pow(x,r,n+1) << "\n";
	}
}
accuracy: 1
time_ms: 110
memory_kb: 4140
final_rank: 3

code_snippet: /* -*- coding: utf-8 -*-
 *
 * 3154.cc: Problem D: Treasure Mountains
 */

#include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
 
using namespace std;

/* constant */

const int MAX_N = 9999990;
const int MAX_M = 50000;

/* typedef */

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vpii;

struct MI {
  static int MOD;
  int v;
  MI(): v() {}
  MI(int _v): v(_v % MOD) {}
  MI(long long _v): v(_v % MOD) {}

  MI operator+(const MI m) const { return MI(v + m.v); }
  MI operator-(const MI m) const { return MI(v + MOD - m.v); }
  MI operator*(const MI m) const { return MI((long long)v * m.v); }

  MI &operator+=(const MI m) { return (*this = *this + m); }
  MI &operator-=(const MI m) { return (*this = *this - m); }
  MI &operator*=(const MI m) { return (*this = *this * m); }

  bool operator==(const MI m) const { return v == m.v; }
  bool operator!=(const MI m) const { return v != m.v; }

  MI pow(int n) const {  // a^n % MOD
    MI pm = 1, a = *this;
    while (n > 0) {
      if (n & 1) pm *= a;
      a *= a;
      n >>= 1;
    }
    return pm;
  }

  MI inv() const { return pow(MOD - 2); }
  MI operator/(const MI m) const { return *this * m.inv(); }
  MI &operator/=(const MI m) { return (*this = *this / m); }
};

typedef MI mi;
int MI::MOD;

struct SegTreeMul {
  typedef int T;
  typedef long long ll;
  int e2;
  vector<T> nodes;
  T defv, mod;
  SegTreeMul() {}

  void init(int n, T _defv, T _mod) {
    defv = _defv, mod = _mod;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = (ll)nodes[j * 2 + 1] * nodes[j * 2 + 2] % mod;
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = (ll)nodes[j * 2 + 1] * nodes[j * 2 + 2] % mod;
    }
  }

  T mul_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = mul_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = mul_range(r0, r1, k * 2 + 2, im, i1);
    return (ll)v0 * v1 % mod;
  }
  T mul_range(int r0, int r1) { return mul_range(r0, r1, 0, 0, e2); }
};

/* global variables */

bool primes[MAX_N + 1];
int ds[MAX_M];
SegTreeMul st;

/* subroutines */

int gen_primes(int maxp, vi &pnums) {
  fill(primes, primes + maxp + 1, true);
  primes[0] = primes[1] = false;

  int p;
  for (p = 2; p * p <= maxp; p++)
    if (primes[p]) {
      pnums.push_back(p);
      for (int q = p * p; q <= maxp; q += p) primes[q] = false;
    }
  for (; p <= maxp; p++)
    if (primes[p]) pnums.push_back(p);
  return (int)pnums.size();
}

bool prime_decomp(int n, vi &pnums, vpii& pds) {
  pds.clear();

  int pn = pnums.size();
  for (int i = 0; i < pn; i++) {
    int pi = pnums[i];
    if (pi * pi > n) {
      if (n > 1) pds.push_back(pii(n, 1));
      return true;
    }

    if (n % pi == 0) {
      int fi = 0;
      while (n % pi == 0) n /= pi, fi++;
      pds.push_back(pii(pi, fi));
    }
  }
  return false;
}

int powi(int a, int n) {  // a^n
  int pm = 1;
  while (n > 0) {
    if (n & 1) pm *= a;
    a *= a;
    n >>= 1;
  }
  return pm;
}

int phi(int n, vi &pnums) {
  vpii pds;
  prime_decomp(n, pnums, pds);

  int r = 1;
  for (auto pd: pds)
    r *= powi(pd.first, pd.second) - powi(pd.first, pd.second - 1);
  return r;
}

int powmod(int a, int n, int mod) {  // a^n % MOD
  int pm = 1;
  while (n > 0) {
    if (n & 1) pm = (ll)pm * a % mod;
    a = (ll)a * a % mod;
    n >>= 1;
  }
  return pm;
}

/* main */

int main() {
  vi pnums;
  gen_primes(MAX_N, pnums);

  int n, m, qn;
  scanf("%d%d%d", &n, &m, &qn);
  MI::MOD = n + 1;

  int ph = phi(n, pnums);
  //printf("phi(%d) = %d\n", n, ph);

  for (int i = 0; i < m; i++) scanf("%d", ds + i);

  st.init(m * 2, 1, n);
  for (int i = 0; i < m; i++)
    st.seti(i, ds[i]), st.seti(m + i, ds[i]);
  st.setall();

  int dm = st.mul_range(0, m);

  while (qn--) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z), y--;
    //printf("x=%d, y=%d, z=%d\n", x, y, z);

    int zq = z / m, zr = z % m;
    int e = (ll)powmod(dm, zq, n) * st.mul_range(y, y + zr) % n;

    // t^e=x, if e*f=1(%n) -> t=x^f
    // e*f=1(%n) -> f=1/e(%n)
    int f = powmod(e, ph - 1, n);
    //printf("  t^%d=%d -> f=%d\n", e, x, f);

    mi t = mi(x).pow(f);
    printf("%d\n", t.v);
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 18340
final_rank: 10

code_snippet: // clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll g = a;
    x = 1;
    y = 0;
    if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
    return g;
}

// a^(-1) mod m
// aとmは互いに素でなければならない
ll mod_inverse(ll a, ll m) {
    assert(__gcd(a, m) == 1);
    ll x, y;
    ll g = extgcd(a, m, x, y);
    while (x < 0) x += m;
    return x % m;
}

ll mod_pow(ll x, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) (ret *= x) %= mod;
        n >>= 1;
        (x *= x) %= mod;
    }
    return ret;
}

const int N = 10000000;
int n, m, q;
int d[N];

const int B = 250;
ll v[B][B];
ll b[B];

ll calc(int s, int e) {
    int sb = s / B, eb = e / B;
    ll ret = 1;
    if (s <= e) {
        if (sb == eb) {
            for (int i = s; i <= e; ++i) (ret *= v[sb][i % B]) %= n;
        } else {
            for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
            for (int i = sb + 1; i < eb; ++i) (ret *= b[i]) %= n;
            for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
        }
    } else {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < B; ++i) (ret *= b[i]) %= n;
        for (int i = 0; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    }
    return ret;
}

int main() {
    rep(i, B) rep(j, B) v[i][j] = 1;

    scanf(" %d %d %d", &n, &m, &q);
    rep(i, m) {
        scanf(" %d", &d[i]);
        v[i / B][i % B] = d[i];
    }

    ll prod_n = 1;
    rep(i, m)(prod_n *= d[i]) %= n;

    rep(i, B) {
        b[i] = 1;
        rep(j, B)(b[i] *= v[i][j]) %= n;
    }

    rep(qi, q) {
        int x, y, z;
        scanf(" %d %d %d", &x, &y, &z);

        --y;

        ll w = mod_pow(prod_n, z / m, n);
        z %= m;
        if (z) (w *= calc(y, (y + z - 1) % m)) %= n;

        // t^w = x (mod n+1)
        // x^r = tとして、 x = x^(r*w) (mod n+1)
        // r*w = 1 (mod n)
        ll r = mod_inverse(w, n);
        ll t = mod_pow(x, r, n + 1);
        printf("%lld\n", t);
    }
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 3900
final_rank: 9

code_snippet: // clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll g = a;
    x = 1;
    y = 0;
    if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
    return g;
}

// a^(-1) mod m
// aとmは互いに素でなければならない
ll mod_inverse(ll a, ll m) {
    assert(__gcd(a, m) == 1);
    ll x, y;
    ll g = extgcd(a, m, x, y);
    while (x < 0) x += m;
    return x % m;
}

ll mod_pow(ll x, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) (ret *= x) %= mod;
        n >>= 1;
        (x *= x) %= mod;
    }
    return ret;
}

const int N = 10000000;
int n, m, q;
int d[N];

const int B = 3180;
ll v[B][B];
ll b[B];

ll calc(int s, int e) {
    int sb = s / B, eb = e / B;
    ll ret = 1;
    if (s < e) {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    } else {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < B; ++i) (ret *= b[i]) %= n;
        for (int i = 0; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    }
    return ret;
}

int main() {
    rep(i, B) rep(j, B) v[i][j] = 1;

    scanf(" %d %d %d", &n, &m, &q);
    rep(i, m) {
        scanf(" %d", &d[i]);
        v[i / B][i % B] = d[i];
    }

    ll prod_n = 1;
    rep(i, m)(prod_n *= d[i]) %= n;

    rep(i, B) {
        b[i] = 1;
        rep(j, B)(b[i] *= v[i][j]) %= n;
    }

    rep(qi, q) {
        int x, y, z;
        scanf(" %d %d %d", &x, &y, &z);

        --y;

        ll w = mod_pow(prod_n, z / m, n);
        z %= m;
        if (z) (w *= calc(y, (y + z - 1) % m)) %= n;

        // t^w = x (mod n+1)
        // x^r = tとして、 x = x^(r*w) (mod n+1)
        // r*w = 1 (mod n)
        ll r = mod_inverse(w, n);
        ll t = mod_pow(x, r, n + 1);
        printf("%lld\n", t);
    }
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 150
memory_kb: 82236
final_rank: 18

code_snippet: // clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll g = a;
    x = 1;
    y = 0;
    if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
    return g;
}

// a^(-1) mod m
// aとmは互いに素でなければならない
ll mod_inverse(ll a, ll m) {
    assert(__gcd(a, m) == 1);
    ll x, y;
    ll g = extgcd(a, m, x, y);
    while (x < 0) x += m;
    return x % m;
}

ll mod_pow(ll x, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) (ret *= x) %= mod;
        n >>= 1;
        (x *= x) %= mod;
    }
    return ret;
}

const int N = 10000000;
int n, m, q;
int d[N];

const int B = 3180;
ll v[B][B];
ll b[B];

ll calc(int s, int e) {
    int sb = s / B, eb = e / B;
    ll ret = 1;
    if (s < e) {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    } else {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < B; ++i) (ret *= b[i]) %= n;
        for (int i = 0; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    }
    return ret;
}

int main() {
    rep(i, B) rep(j, B) v[i][j] = 1;

    scanf(" %d %d %d", &n, &m, &q);
    rep(i, m) {
        scanf(" %d", &d[i]);
        v[i / B][i % B] = d[i];
    }

    ll prod_n = 1;
    rep(i, m)(prod_n *= d[i]) %= n;

    rep(i, B) {
        b[i] = 1;
        rep(j, B)(b[i] *= v[i][j]) %= n;
    }

    rep(qi, q) {
        int x, y, z;
        scanf(" %d %d %d", &x, &y, &z);

        --y;

        ll w = mod_pow(prod_n, z / n, n);
        z %= n;
        if (z) (w *= calc(y, (y + z - 1) % m)) %= n;

        // t^w = x (mod n+1)
        // x^r = tとして、 x = x^(r*w) (mod n+1)
        // r*w = 1 (mod n)
        ll r = mod_inverse(w, n);
        ll t = mod_pow(x, r, n + 1);
        printf("%lld\n", t);
    }
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 150
memory_kb: 82236
final_rank: 18

code_snippet: // clang-format off
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
// clang-format on

ll extgcd(ll a, ll b, ll& x, ll& y) {
    ll g = a;
    x = 1;
    y = 0;
    if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
    return g;
}

// a^(-1) mod m
// aとmは互いに素でなければならない
ll mod_inverse(ll a, ll m) {
    assert(__gcd(a, m) == 1);
    ll x, y;
    ll g = extgcd(a, m, x, y);
    while (x < 0) x += m;
    return x % m;
}

ll mod_pow(ll x, ll n, ll mod) {
    ll ret = 1;
    while (n) {
        if (n & 1) (ret *= x) %= mod;
        n >>= 1;
        (x *= x) %= mod;
    }
    return ret;
}

const int N = 10000000;
int n, m, q;
int d[N];

const int B = 3180;
ll v[B][B];
ll b[B];

ll calc(int s, int e) {
    int sb = s / B, eb = e / B;
    ll ret = 1;
    if (s < e) {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    } else {
        for (int i = s; i < (sb + 1) * B; ++i) (ret *= v[sb][i % B]) %= n;
        for (int i = sb + 1; i < B; ++i) (ret *= b[i]) %= n;
        for (int i = 0; i < eb; ++i) (ret *= b[i]) %= n;
        for (int i = eb * B; i <= e; ++i) (ret *= v[eb][i % B]) %= n;
    }
    return ret;
}

int main() {
    rep(i, B) rep(j, B) v[i][j] = 1;

    scanf(" %d %d %d", &n, &m, &q);
    rep(i, m) {
        scanf(" %d", &d[i]);
        v[i / B][i % B] = d[i];
    }

    ll prod_n = 1;
    rep(i, m)(prod_n *= d[i]) %= n;

    rep(i, B) {
        b[i] = 1;
        rep(j, B)(b[i] *= v[i][j]) %= n;
    }

    rep(qi, q) {
        int x, y, z;
        scanf(" %d %d %d", &x, &y, &z);

        --y;

        ll w = mod_pow(prod_n, z / n, n);
        z %= n;
        if (z) (w *= calc(y, (y + z - 1) % n)) %= n;

        // t^w = x (mod n+1)
        // x^r = tとして、 x = x^(r*w) (mod n+1)
        // r*w = 1 (mod n)
        ll r = mod_inverse(w, n);
        ll t = mod_pow(x, r, n + 1);
        printf("%lld\n", t);
    }
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 150
memory_kb: 82264
final_rank: 20

code_snippet: #include <iostream>
#include <vector>


#include <utility>

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct barrett {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m < 2^31`
    barrett(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned int v = (unsigned int)(z - x * _m);
        if (_m <= v) v += _m;
        return v;
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

}  // namespace internal

}  // namespace atcoder


#include <cassert>
#include <numeric>
#include <type_traits>

namespace atcoder {

namespace internal {

#ifndef _MSC_VER
template <class T>
using is_signed_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value ||
                                  std::is_same<T, __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int128 =
    typename std::conditional<std::is_same<T, __uint128_t>::value ||
                                  std::is_same<T, unsigned __int128>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using make_unsigned_int128 =
    typename std::conditional<std::is_same<T, __int128_t>::value,
                              __uint128_t,
                              unsigned __int128>;

template <class T>
using is_integral = typename std::conditional<std::is_integral<T>::value ||
                                                  is_signed_int128<T>::value ||
                                                  is_unsigned_int128<T>::value,
                                              std::true_type,
                                              std::false_type>::type;

template <class T>
using is_signed_int = typename std::conditional<(is_integral<T>::value &&
                                                 std::is_signed<T>::value) ||
                                                    is_signed_int128<T>::value,
                                                std::true_type,
                                                std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<(is_integral<T>::value &&
                               std::is_unsigned<T>::value) ||
                                  is_unsigned_int128<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<
    is_signed_int128<T>::value,
    make_unsigned_int128<T>,
    typename std::conditional<std::is_signed<T>::value,
                              std::make_unsigned<T>,
                              std::common_type<T>>::type>::type;

#else

template <class T> using is_integral = typename std::is_integral<T>;

template <class T>
using is_signed_int =
    typename std::conditional<is_integral<T>::value && std::is_signed<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using is_unsigned_int =
    typename std::conditional<is_integral<T>::value &&
                                  std::is_unsigned<T>::value,
                              std::true_type,
                              std::false_type>::type;

template <class T>
using to_unsigned = typename std::conditional<is_signed_int<T>::value,
                                              std::make_unsigned<T>,
                                              std::common_type<T>>::type;

#endif

template <class T>
using is_signed_int_t = std::enable_if_t<is_signed_int<T>::value>;

template <class T>
using is_unsigned_int_t = std::enable_if_t<is_unsigned_int<T>::value>;

template <class T> using to_unsigned_t = typename to_unsigned<T>::type;

}  // namespace internal

}  // namespace atcoder

#include <cassert>
#include <numeric>
#include <type_traits>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

struct modint_base {};
struct static_modint_base : modint_base {};

template <class T> using is_modint = std::is_base_of<modint_base, T>;
template <class T> using is_modint_t = std::enable_if_t<is_modint<T>::value>;

}  // namespace internal

template <int m, std::enable_if_t<(1 <= m)>* = nullptr>
struct static_modint : internal::static_modint_base {
    using mint = static_modint;

  public:
    static constexpr int mod() { return m; }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    static_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    static_modint(T v) {
        long long x = (long long)(v % (long long)(umod()));
        if (x < 0) x += umod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    static_modint(T v) {
        _v = (unsigned int)(v % umod());
    }
    static_modint(bool v) { _v = ((unsigned int)(v) % umod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v -= rhs._v;
        if (_v >= umod()) _v += umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        unsigned long long z = _v;
        z *= rhs._v;
        _v = (unsigned int)(z % umod());
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        if (prime) {
            assert(_v);
            return pow(umod() - 2);
        } else {
            auto eg = internal::inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static constexpr unsigned int umod() { return m; }
    static constexpr bool prime = internal::is_prime<m>;
};

template <int id> struct dynamic_modint : internal::modint_base {
    using mint = dynamic_modint;

  public:
    static int mod() { return (int)(bt.umod()); }
    static void set_mod(int m) {
        assert(1 <= m);
        bt = internal::barrett(m);
    }
    static mint raw(int v) {
        mint x;
        x._v = v;
        return x;
    }

    dynamic_modint() : _v(0) {}
    template <class T, internal::is_signed_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        long long x = (long long)(v % (long long)(mod()));
        if (x < 0) x += mod();
        _v = (unsigned int)(x);
    }
    template <class T, internal::is_unsigned_int_t<T>* = nullptr>
    dynamic_modint(T v) {
        _v = (unsigned int)(v % mod());
    }
    dynamic_modint(bool v) { _v = ((unsigned int)(v) % mod()); }

    unsigned int val() const { return _v; }

    mint& operator++() {
        _v++;
        if (_v == umod()) _v = 0;
        return *this;
    }
    mint& operator--() {
        if (_v == 0) _v = umod();
        _v--;
        return *this;
    }
    mint operator++(int) {
        mint result = *this;
        ++*this;
        return result;
    }
    mint operator--(int) {
        mint result = *this;
        --*this;
        return result;
    }

    mint& operator+=(const mint& rhs) {
        _v += rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator-=(const mint& rhs) {
        _v += mod() - rhs._v;
        if (_v >= umod()) _v -= umod();
        return *this;
    }
    mint& operator*=(const mint& rhs) {
        _v = bt.mul(_v, rhs._v);
        return *this;
    }
    mint& operator/=(const mint& rhs) { return *this = *this * rhs.inv(); }

    mint operator+() const { return *this; }
    mint operator-() const { return mint() - *this; }

    mint pow(long long n) const {
        assert(0 <= n);
        mint x = *this, r = 1;
        while (n) {
            if (n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    mint inv() const {
        auto eg = internal::inv_gcd(_v, mod());
        assert(eg.first == 1);
        return eg.second;
    }

    friend mint operator+(const mint& lhs, const mint& rhs) {
        return mint(lhs) += rhs;
    }
    friend mint operator-(const mint& lhs, const mint& rhs) {
        return mint(lhs) -= rhs;
    }
    friend mint operator*(const mint& lhs, const mint& rhs) {
        return mint(lhs) *= rhs;
    }
    friend mint operator/(const mint& lhs, const mint& rhs) {
        return mint(lhs) /= rhs;
    }
    friend bool operator==(const mint& lhs, const mint& rhs) {
        return lhs._v == rhs._v;
    }
    friend bool operator!=(const mint& lhs, const mint& rhs) {
        return lhs._v != rhs._v;
    }

  private:
    unsigned int _v;
    static internal::barrett bt;
    static unsigned int umod() { return bt.umod(); }
};
template <int id> internal::barrett dynamic_modint<id>::bt = 998244353;

using modint998244353 = static_modint<998244353>;
using modint1000000007 = static_modint<1000000007>;
using modint = dynamic_modint<-1>;

namespace internal {

template <class T>
using is_static_modint = std::is_base_of<internal::static_modint_base, T>;

template <class T>
using is_static_modint_t = std::enable_if_t<is_static_modint<T>::value>;

template <class> struct is_dynamic_modint : public std::false_type {};
template <int id>
struct is_dynamic_modint<dynamic_modint<id>> : public std::true_type {};

template <class T>
using is_dynamic_modint_t = std::enable_if_t<is_dynamic_modint<T>::value>;

}  // namespace internal

}  // namespace atcoder


namespace ac = atcoder;

using mint0 = ac::dynamic_modint<0>;
using mint1 = ac::dynamic_modint<1>;

void solve() {
    int n, m, q;
    std::cin >> n >> m >> q;

    mint0::set_mod(n + 0);
    mint1::set_mod(n + 1);

    std::vector<mint0> prods(m + 1);  // 累積積
    prods[0] = 1;
    for (int i = 1; i <= m; ++i) {
        int d;
        std::cin >> d;
        prods[i] = prods[i - 1] * d;
    }

    // [0, len)の積を返す
    auto getprod = [&](int len) {
        return prods[m].pow(len / m) * prods[len % m];
    };

    while (q--) {
        int x, y, z;
        std::cin >> x >> y >> z;
        --y;

        auto p = getprod(y + z) / getprod(y);
        std::cout << mint1(x).pow(p.inv().val()).val() << "\n";
    }
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3500
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;

// as + bt = GCD(a,b) a,b:const s,t:var(any)
// return GCD(a,b)
long long extGCD(long long a, long long b, long long& s, long long& t) {
  s = 1, t = 0;
  long long u = 0, v = 1;
  while (b) {
    long long tmp = a / b;
    a -= b * tmp;
    s -= u * tmp;
    t -= v * tmp;
    swap(s, u);
    swap(t, v);
    swap(a, b);
  }
  return a;
}
// (mod)x+ay=1, calculate y -> a^-1 (mod m) (a,m : coprime)
long long calcinv(long long a, long long m) {
  long long s, t;
  extGCD(a, m, s, t);
  return (s + m) % m;
}

long long mod_pow(long long base, int e, int mod) {
  long long res = 1;
  while (e) {
    if (e & 1) (res *= base) %= mod;
    (base *= base) %= mod;
    e >>= 1;
  }
  return res;
}

int n, m, q, all = 1;
vector<int> mul;

int main() {
  cin >> n >> m >> q;
  {  // input and pre calc
    mul.assign(2 * m + 1, 1);
    for (int i = 1; i <= 2 * m; ++i) {
      if (i <= m) {
        cin >> mul[i];
        mul[i] = mul[i + m] = calcinv(mul[i], n);
        all = 1LL * all * mul[i] % n;
      }
      mul[i] = 1LL * mul[i - 1] * mul[i] % n;
    }
  }
  auto calc = [](int l, int r) {
    return 1LL * mul[r] * calcinv(mul[l], n) % n;
  };
  for (int i = 0; i < q; ++i) {
    int x, y, z, e;
    cin >> x >> y >> z;
    --y;
    e = 1LL * mod_pow(all, z / m, n) * calc(y, y + z % m) % n;
    cout << mod_pow(x, e, n + 1) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3548
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

// 0-indexed
template <class T>
struct SegmentTree {
  // a,b,c: T, e:T(unit)
  // abc = (ab)c = a(bc)
  // ae = ea = a
  typedef function<T(T, T)> F;
  int n;
  F f;
  T unit;
  vector<T> dat;
  SegmentTree(){};
  SegmentTree(int newn, F f, T t) : f(f), unit(t) { init(newn); }
  SegmentTree(const vector<T>& v, F f, T t) : f(f), unit(t) {
    int _n = v.size();
    init(v.size());
    for (int i = 0; i < _n; ++i) dat[n + i] = v[i];
    for (int i = n - 1; i; --i) dat[i] = f(dat[i << 1], dat[(i << 1) | 1]);
  }
  void init(int newn) {
    n = 1;
    while (n < newn) n <<= 1;
    dat.assign(n << 1, unit);
  }

  // "go up" process
  void update(int k, T newdata) {
    dat[k += n] = newdata;
    while (k >>= 1) {
      dat[k] = f(dat[(k << 1) | 0], dat[(k << 1) | 1]);
    }
  }
  // [a,b)
  T query(int a, int b) {
    T vl = unit, vr = unit;
    for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
      if (l & 1) vl = f(vl, dat[l++]);
      if (r & 1) vr = f(dat[--r], vr);
    }
    return f(vl, vr);
  }
};

// as + bt = GCD(a,b) a,b:const s,t:var(any)
// return GCD(a,b)
long long extGCD(long long a, long long b, long long& s, long long& t) {
  s = 1, t = 0;
  long long u = 0, v = 1;
  while (b) {
    long long tmp = a / b;
    a -= b * tmp;
    s -= u * tmp;
    t -= v * tmp;
    swap(s, u);
    swap(t, v);
    swap(a, b);
  }
  return a;
}
// (mod)x+ay=1, calculate y -> a^-1 (mod m) (a,m : coprime)
long long calcinv(long long a, long long m) {
  long long s, t;
  extGCD(a, m, s, t);
  return (s + m) % m;
}

long long mod_pow(long long base, int e, int mod) {
  long long res = 1;
  while (e) {
    if (e & 1) (res *= base) %= mod;
    (base *= base) %= mod;
    e >>= 1;
  }
  return res;
}

int n, m, q, all = 1;
SegmentTree<int> seg;

int main() {
  cin >> n >> m >> q;
  {  // input and pre calc
    auto f = [](int l, int r) { return 1LL * l * r % n; };
    vector<int> v(m, 0);
    for (auto& p : v) {
      cin >> p;
      p = calcinv(p, n);
      all = 1LL * all * p % n;
    }
    for (int i = 0; i < m; ++i) v.push_back(v[i]);
    seg = SegmentTree<int>(v, f, 1);
  }
  for (int i = 0; i < q; ++i) {
    int x, y, z, e;
    cin >> x >> y >> z;
    --y;
    e = 1LL * mod_pow(all, z / m, n) * seg.query(y, y + z % m) % n;
    cout << mod_pow(x, e, n + 1) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 4420
final_rank: 4

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define ll long long
#define FOR(i, a, b) for(ll i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n+1)
#define rrep(i, n) for (ll i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define pr(s) cout << (s) << '\n'
ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
const int INF = 1001001001;
const ll INFll = 1E+18;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < mod);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint per(int n, int k) {
        return fact[n]*ifact[n-k];
    }
};

    


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    ll n, m, q;
    cin >> n >> m >> q;
    mod = n;
    //if (n!=10 && n!=970) debug(n);
    //if (n!=10 && n!=970) debug(m);
    //if (n!=10 && n!=970) debug(q);
    vector<ll> d(m);
    rep(i, m) cin >> d[i];
    //if (n!=10 && n!=970) rep(i, m) cerr << i << " " << d[i] << endl;
    vector<mint> sd(m*2+1);
    sd[0] = 1;
    rep(i, 2*m) {
        sd[i+1] = sd[i]*d[i%m];
        /*
        if (n!=10 && n!=970) {
            cerr << "i sd[i]" << i+1 << " " << sd[i+1].x << endl;
        }
        */
        //debug(sd[i+1]);
    }
    vector<ll> inverse(n+1);
    inverse[1] = 1;
    vector<bool> used(n+1);
    for (ll i=2; i<=n; i++) {
        if (used[i]) continue;
        if (__gcd(n, i)==1) {
            ll now = i;
            vector<ll> v;
            while (1) {
                used[now] = true;
                v.pb(now);
                if (now==1) break;
                now *= i;
                now %= n;
            }
            rep(j, v.size()-1) {
                ll zz = lcm(j+1, (ll)v.size());
                ll yy = zz - (j+1);
                inverse[v[j]] = v[(yy-1+v.size())%v.size()];
                if (n!=10) {
                    //debug(v[j]);
                    //debug((yy-1)%n);
                    //debug(inverse[v[j]]);
                }
            }
            //debug(n);
        }
    }
    
    //debug(num_prime);
    rep(qi, q) {
        ll x, y, z;
        cin >> x >> y >> z;
        y--;
        mint p = sd[m].pow(z/m);
        /*
        if (n!=10) {
            cerr << "sd[m] p " << sd[m].x << " " << p.x << endl;
        }
        */
        z %= m;
        p *= sd[y+z]*inverse[sd[y].x];
        /*
        if (n!=10) {
            cerr << "y+z y sd[y+z] sd[y] p " << y+z << " " << y << " " << sd[y+z].x << " " << sd[y].x << " " << p.x << endl;
        }
        */
        //debug(y+z); debug(y);
        //debug(sd[y+z]); debug(sd[y]);
        //debug(p);
        ll inv = inverse[p.x];
        //debug(inv);
        mod = n+1;
        mint ans = mint(x).pow(inv);
        pr(ans);
        mod = n;
        /*
        if (n!=10 && n!=970) {
            cerr << x << " " << y << " " << z << endl;
            cerr << ans << " " << p.x << endl;
            int pp = p.x;
            mod = n+1;
            cerr << ans.pow(p.x) << endl;
            mod = n;
        }
        */
        
    }
    return 0;
}
accuracy: 1
time_ms: 2050
memory_kb: 114716
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;

//////////////////////////////   Begin Macros

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define in(x, a, b) (a <= x and x < b)
#define rep(i, N) for (int i = 0; i < (int)(N); i++)
#define reprev(i, N) for (int i = (int)(N)-1; i >= 0; i--)
#define rep1(i, N) for (int i = 1; i <= (int)(N); i++)
#define rep1rev(i, N) for (int i = (int)(N); i >= 0; i--)
#define forbe(i, b, e) for (int i = (b); i < (e); i++)
#define forberev(i, b, e) for (int i = (e)-1; i >= (b); i--)
#define forfl(i, f, l) for (int i = (f); i <= (l); i++)
#define forflrev(i, f, l) for (int i = (l); i >= (f); i--)
using ll = long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &m, const T q)
{
  if (m < q)
  {
    m = q;
    return true;
  }
  else
    return false;
}
template <typename T>
bool chmin(T &m, const T q)
{
  if (m > q)
  {
    m = q;
    return true;
  }
  else
    return false;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first + r.first, l.second + r.second}; }
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) { return {l.first - r.first, l.second - r.second}; }
template <typename T>
pair<T, T> operator*(const pair<T, T> &l, const T &r) { return {l.first * r, l.second * r}; }
template <typename T>
pair<T, T> operator/(const pair<T, T> &l, const T &r) { return {l.first / r, l.second / r}; }
template <typename T>
istream &operator>>(istream &is, vector<T> &vec)
{
  for (auto &v : vec)
    is >> v;
  return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec)
{
  os << "[";
  for (auto v : vec)
    os << v << ",";
  os << "]";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const deque<T> &vec)
{
  os << "deq[";
  for (auto v : vec)
    os << v << ",";
  os << "]";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const set<T> &vec)
{
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_set<T> &vec)
{
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const multiset<T> &vec)
{
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const unordered_multiset<T> &vec)
{
  os << "{";
  for (auto v : vec)
    os << v << ",";
  os << "}";
  return os;
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &pa)
{
  is >> pa.first >> pa.second;
  return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &pa)
{
  os << "(" << pa.first << "," << pa.second << ")";
  return os;
}
template <typename... Ts>
istream &operator>>(istream &is, tuple<Ts...> &theTuple)
{
  apply([&is](Ts &... tupleArgs) { ((is >> tupleArgs), ...); }, theTuple);
  return is;
}
template <typename... Ts>
ostream &operator<<(ostream &os, const tuple<Ts...> &theTuple)
{
  apply([&os](const Ts &... tupleArgs) {
    os << '(';
    size_t n(0);
    ((os << tupleArgs << (++n < sizeof...(Ts) ? "," : "")), ...);
    os << ')';
  },
        theTuple);
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const map<TK, TV> &mp)
{
  os << "{";
  for (auto v : mp)
    os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}
template <typename TK, typename TV>
ostream &operator<<(ostream &os, const unordered_map<TK, TV> &mp)
{
  os << "{";
  for (auto v : mp)
    os << v.first << "=>" << v.second << ",";
  os << "}";
  return os;
}

template <typename T>
void reset(vector<T> &v, const T reset_to)
{
  for (auto &x : v)
    x = reset_to;
}
inline int popcount(const unsigned int x) { return __builtin_popcount(x); }
#define dbg(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ") " << __FILE__ << endl;

ll nC2(ll n)
{
  return n * (n - 1) / 2;
}

const int intinf = numeric_limits<int>::max();
const ll llinf = numeric_limits<ll>::max();
const pii udlr[4] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

//////////////////////////////   End Macros

// Modulo.h start----------------------------------------

#include <vector>
#include <iostream>
#include <cassert>

using namespace std;

// a x + b y = gcd(a, b)
int extgcd(const int a, const int b, int &x, int &y)
{
  // O(log max(a,b))
  int g = a;
  x = 1;
  y = 0;
  if (b != 0)
    g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

int inv_mod(const int a, const int mod)
{
  int x, y;
  if (extgcd(a, mod, x, y) == 1)
    return (x + mod) % mod;
  else // unsolvable
    return 0;
}

struct Modint
{
  static int mod;
  static bool mod_is_prime;
  long long value;
  Modint(long long value = 0LL) : value((value % mod + mod) % mod) {}
  inline Modint operator+() const { return *this; }
  inline Modint operator-() const { return Modint(-value); }
  inline Modint &operator+=(const Modint &rhs)
  {
    if ((value += rhs.value) >= mod)
      value -= mod;
    return *this;
  }
  inline Modint &operator-=(const Modint &rhs)
  {
    if ((value += mod - rhs.value) >= mod)
      value -= mod;
    return *this;
  }
  inline Modint &operator*=(const Modint &rhs)
  {
    (value *= rhs.value) %= mod;
    return *this;
  }
  Modint pow(long long k) const
  {
    assert(k >= 0);
    if (k == 0)
      return 1;
    Modint res = pow(k / 2);
    res *= res;
    if (k % 2 == 1)
      res *= *this;
    return res;
  }

  Modint inv() const
  {
    if (mod_is_prime)
      return pow(mod - 2);

    // value and mod are coprime
    long long a = value, b = mod, u = 1, v = 0;
    while (b)
    {
      long long t = a / b;
      a -= t * b;
      swap(a, b);
      u -= t * v;
      swap(u, v);
    }
    u %= mod;
    if (u < 0)
      u += mod;
    return u;
  }
  inline Modint &operator/=(const Modint &rhs) { return *this *= rhs.inv(); }
};
int Modint::mod = 1000000007;
bool Modint::mod_is_prime = true;
bool operator==(const Modint &lhs, const Modint &rhs) { return lhs.value == rhs.value; }
bool operator!=(const Modint &lhs, const Modint &rhs) { return !(lhs == rhs); }
Modint operator+(const Modint &lhs, const Modint &rhs) { return Modint(lhs) += rhs; }
Modint operator-(const Modint &lhs, const Modint &rhs) { return Modint(lhs) -= rhs; }
Modint operator*(const Modint &lhs, const Modint &rhs) { return Modint(lhs) *= rhs; }
Modint operator/(const Modint &lhs, const Modint &rhs) { return Modint(lhs) /= rhs; }
istream &operator>>(istream &lhs, Modint &rhs) { return lhs >> rhs.value; }
ostream &operator<<(ostream &lhs, const Modint &rhs) { return lhs << rhs.value; }

struct Combination
{
  std::vector<Modint> fact, ifact;
  Combination(int n = 0) : fact(n + 1), ifact(n + 1)
  {
    assert(n >= 0);
    fact[0] = 1;
    for (int i = 1; i <= n; i++)
      fact[i] = fact[i - 1] * i;
    ifact[n] = fact[n].inv();
    for (int i = n; i >= 1; i--)
      ifact[i - 1] = ifact[i] * i;
  }
  Modint operator()(int n, int k)
  {
    assert(0 <= n and n <= (int)fact.size());
    if (!(0 <= k and k <= n))
      return 0;
    return fact[n] * ifact[k] * ifact[n - k];
  }
};

// Modulo.h end----------------------------------------------

void solve()
{
  int N, M, Q;
  cin >> N >> M >> Q;
  vector<int> d(M);
  cin >> d;

  Modint::mod = N;
  Modint::mod_is_prime = false;
  vector<Modint> sd(2 * M + 1);
  sd[0] = 1;
  rep(i, 2 * M) sd[i + 1] = sd[i] * d[i % M];

  rep(_, Q)
  {
    int x, y, z;
    cin >> x >> y >> z;
    y--;

    Modint::mod = N;
    Modint::mod_is_prime = false;
    int q = z / M;
    int r = z % M;
    Modint a = sd[M].pow(q) * sd[y + r] / sd[y];
    auto b = a.inv().value;

    Modint::mod = N + 1;
    Modint::mod_is_prime = true;
    Modint t = ((Modint)(x)).pow(b);
    cout << t << endl;
  }
}

int main()
{
  // cerr << "start" << endl;
  // srand(time(0));
  cout << fixed << setprecision(15);

  solve();
  return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 3996
final_rank: 5

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
#define ll long long
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, n+1)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; --i)
#define whole(x) (x).begin(),(x).end()
#define rwhole(x) (x).rbegin(), (x).rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define P pair<int, int>
#define debug(var) cerr << "[" << #var << "] " << var << endl
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
#define vi vector<int>
#define vl vector<ll>
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define pr(s) cout << (s) << '\n'
ll mod = 1000000007;
const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
const int INF = 1001001001;
const ll INFll = 1E+18;

struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%mod+mod)%mod){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) {
        (x *= a.x) %= mod;
        return *this;
    }
    mint operator+(const mint a) const {
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a) const {
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a) const {
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    
    // for prime mod
    mint inv() const {
        return pow(mod-2);
    }
    mint& operator/=(const mint a) {
        return (*this) *= a.inv();
    }
    mint operator/(const mint a) const {
        mint res(*this);
        return res/=a;
    }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < mod);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint per(int n, int k) {
        return fact[n]*ifact[n-k];
    }
};

    


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int n, m, q;
    cin >> n >> m >> q;
    mod = n;
    //if (n!=10 && n!=970) debug(n);
    //if (n!=10 && n!=970) debug(m);
    //if (n!=10 && n!=970) debug(q);
    vector<int> d(m);
    rep(i, m) cin >> d[i];
    //if (n!=10 && n!=970) rep(i, m) cerr << i << " " << d[i] << endl;
    vector<mint> sd(2*m+1);
    sd[0] = 1;
    rep(i, 2*m) {
        sd[i+1] = sd[i]*d[i%m];
        /*
        if (n!=10 && n!=970) {
            cerr << "i sd[i]" << i+1 << " " << sd[i+1].x << endl;
        }
        */
        //debug(sd[i+1]);
    }
    vector<int> inverse(n+1);
    inverse[1] = 1;
    vector<bool> used(n+1);
    for (int i=2; i<=n; i++) {
        if (used[i]) continue;
        if (__gcd(n, i)==1) {
            ll now = i;
            vector<int> v;
            while (1) {
                used[now] = true;
                v.pb(now);
                if (now==1) break;
                now *= i;
                now %= n;
            }
            rep(j, v.size()-1) {
                ll zz = lcm(j+1, (int)v.size());
                ll yy = zz - (j+1);
                inverse[v[j]] = v[(yy-1+v.size())%v.size()];
                if (n!=10) {
                    //debug(v[j]);
                    //debug((yy-1)%n);
                    //debug(inverse[v[j]]);
                }
            }
            //debug(n);
        }
    }
    
    //debug(num_prime);
    rep(qi, q) {
        int x, y, z;
        cin >> x >> y >> z;
        y--;
        mint p = sd[m].pow(z/m);
        /*
        if (n!=10) {
            cerr << "sd[m] p " << sd[m].x << " " << p.x << endl;
        }
        */
        z %= m;
        p *= sd[y+z]*inverse[sd[y].x];
        /*
        if (n!=10) {
            cerr << "y+z y sd[y+z] sd[y] p " << y+z << " " << y << " " << sd[y+z].x << " " << sd[y].x << " " << p.x << endl;
        }
        */
        //debug(y+z); debug(y);
        //debug(sd[y+z]); debug(sd[y]);
        //debug(p);
        int inv = inverse[p.x];
        //debug(inv);
        mod = n+1;
        mint ans = mint(x).pow(inv);
        pr(ans);
        mod = n;
        /*
        if (n!=10 && n!=970) {
            cerr << x << " " << y << " " << z << endl;
            cerr << ans << " " << p.x << endl;
            int pp = p.x;
            mod = n+1;
            cerr << ans.pow(p.x) << endl;
            mod = n;
        }
        */
        
    }
    return 0;
}
accuracy: 0.3103448275862069
time_ms: 1420
memory_kb: 46712
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;

const int MAX = 10000000;
int MOD;

long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a / b * x;
    return d;
}

inline long long mod(long long a, long long m=MOD) {
    return (a % m + m) % m;
}

long long modinv(long long a, long long m=MOD) {
    long long x, y;
    extGCD(a, m, x, y);
    return mod(x, m);
}

//Nの剰余でのpow
ll modpow(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow(n, k-1)*n % MOD;
    }
    else{
        ll temp = modpow(n, k/2);
        return temp*temp % MOD;
    }
}

//N+1の剰余でのpow
ll modpow2(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow2(n, k-1)*n % (MOD+1);
    }
    else{
        ll temp = modpow2(n, k/2);
        return temp*temp % (MOD+1);
    }
}

int main(void){
    //A[y-1]*A[y]*...*A[y+z-1] = aとすると,
    //pow(t, a) = x かつ a は gcd(a, N) = 1 より剰余環Z/NZ上で乗法逆元が存在するので pow(x, a^(-1)) が唯一の答え
    int N, M, Q; cin >> N >> M >> Q;
    MOD = N;
    vector<ll> A(M);
    rep(i, M){
        ll a; cin >> a;
        A[i] = a;
    }
    vector<ll> acc_left(M*2+1, 1); //acc_left[i]: A[0]~A[i-1]までの積 (mod N)
    for(int i = 1; i <= M*2; i++){
        acc_left[i] = acc_left[i-1]*A[(i-1) % M] % N;
    } 

    vector<ll> ans;
    rep(i, Q){
        ll x, y, z; cin >> x >> y >> z;
        //A[y-1]*A[y]*...*A[y-1+part-1]を計算
        ll whole = (z - (z % M)) / M; //鍵を完全に周回する回数
        ll part = z - whole*M; //端数
        ll acc = modpow(acc_left[M], whole);
        
        acc = (acc * (acc_left[y-1+part] * modinv(acc_left[y-1]) % N)) % N;
        ans.push_back(modpow2(x, modinv(acc)));
    }
    rep(i, Q) cout << ans[i] << endl;
    return 0;
}
accuracy: 1
time_ms: 120
memory_kb: 4760
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;

const int MAX = 10000000;
int MOD;

long long inv[MAX];

// テーブルを作る前処理
void COMinit() {
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
    }
}

//Nの剰余でのpow
ll modpow(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow(n, k-1)*n % MOD;
    }
    else{
        ll temp = modpow(n, k/2);
        return temp*temp % MOD;
    }
}

//N+1の剰余でのpow
ll modpow2(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow2(n, k-1)*n % (MOD+1);
    }
    else{
        ll temp = modpow2(n, k/2);
        return temp*temp % (MOD+1);
    }
}

int main(void){
    //A[y-1]*A[y]*...*A[y+z-1] = aとすると,
    //pow(t, a) = x かつ a は gcd(a, N) = 1 より剰余環Z/NZ上で乗法逆元が存在するので pow(x, a^(-1)) が唯一の答え
    int N, M, Q; cin >> N >> M >> Q;
    MOD = N;
    COMinit();
    vector<ll> A(M);
    rep(i, M){
        ll a; cin >> a;
        A[i] = a;
    }
    vector<ll> acc_left(M*2+1, 1); //acc_left[i]: A[0]~A[i-1]までの積 (mod N)
    for(int i = 1; i <= M*2; i++){
        acc_left[i] = acc_left[i-1]*A[(i-1) % M] % N;
    } 

    vector<ll> ans;
    rep(i, Q){
        ll x, y, z; cin >> x >> y >> z;
        //A[y-1]*A[y]*...*A[y-1+part-1]を計算
        ll whole = (z - (z % M)) / M; //鍵を完全に周回する回数
        ll part = z - whole*M; //端数
        ll acc = modpow(acc_left[M], whole);
        acc = (acc * (acc_left[y-1+part] * inv[acc_left[y-1]] % N)) % N;
        ans.push_back(modpow2(x, inv[acc]));
    }
    rep(i, Q) cout << ans[i] << endl;
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 90
memory_kb: 81572
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;

const int MAX = 10000000;
int MOD;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
    }
}

//Nの剰余でのpow
ll modpow(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow(n, k-1)*n % MOD;
    }
    else{
        ll temp = modpow(n, k/2);
        return temp*temp % MOD;
    }
}

//N+1の剰余でのpow
ll modpow2(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow2(n, k-1)*n % (MOD+1);
    }
    else{
        ll temp = modpow2(n, k/2);
        return temp*temp % (MOD+1);
    }
}

int main(void){
    //A[y-1]*A[y]*...*A[y+z-1] = aとすると,
    //pow(t, a) = x かつ a は gcd(a, N) = 1 より剰余環Z/NZ上で乗法逆元が存在するので pow(x, a^(-1)) が唯一の答え
    int N, M, Q; cin >> N >> M >> Q;
    MOD = N;
    COMinit();
    vector<ll> Ainv(M);
    rep(i, M){
        ll a; cin >> a;
        Ainv[i] = inv[a];
    }
    vector<ll> acc_left(M*2+1, 1); //acc_left[i]: Ainv[0]~Ainv[i-1]までの積 (mod N)
    for(int i = 1; i <= M*2; i++){
        acc_left[i] = acc_left[i-1]*Ainv[(i-1) % M] % MOD;
    } 

    vector<ll> ans;
    rep(i, Q){
        ll x, y, z; cin >> x >> y >> z;
        //Ainv[y-1]*Ainv[y]*...*Ainv[y-1+part-1]を計算
        ll whole = (z - (z % M)) / M; //鍵を完全に周回する回数
        ll part = z - whole*M; //端数
        ll acc = modpow(acc_left[M], whole);
        acc = (acc * (acc_left[y-1+part] * inv[acc_left[y-1]] % MOD)) % MOD;
        //ll acc = 1;
        //for(int i = y-1, cnt = 0; cnt < z; i = (i+1) % M, cnt++) acc = acc*Ainv[i] % N; これでもダメ
        ans.push_back(modpow2(x, acc));
    }
    rep(i, Q) cout << ans[i] << endl;
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 110
memory_kb: 81312
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;

const int MAX = 1000000;
int MOD;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

//Nの剰余でのpow
ll modpow(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow(n, k-1)*n % MOD;
    }
    else{
        ll temp = modpow(n, k/2);
        return temp*temp % MOD;
    }
}

//N+1の剰余でのpow
ll modpow2(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow2(n, k-1)*n % (MOD+1);
    }
    else{
        ll temp = modpow2(n, k/2);
        return temp*temp % (MOD+1);
    }
}

//これを使うときにはCOMinit();を忘れずに

int main(void){
    int N, M, Q; cin >> N >> M >> Q;
    MOD = N;
    COMinit();
    vector<ll> Ainv(M);
    rep(i, M){
        ll a; cin >> a;
        Ainv[i] = inv[a];
    }
    vector<ll> acc_left(M*2+1, 1);
    for(int i = 1; i <= M*2; i++){
        acc_left[i] = acc_left[i-1]*Ainv[(i-1) % M] % MOD;
    } 
    vector<ll> ans;
    rep(i, Q){
        ll x, y, z; cin >> x >> y >> z;
        //Ainv[y-1]*Ainv[y]*...*Ainv[y-1+part-1]を計算
        ll whole = (z - (z % M)) / M; //巻物を完全に周回する回数
        ll part = z - whole*M; //端数
        //ll acc = modpow(acc_left[M], whole);
        ll acc = 1;
        for(int i = y-1, cnt = 0; cnt < z; i = (i+1) % M, cnt++) acc = acc*Ainv[i] % N;
        //acc = (acc * (acc_left[y-1+part] * inv[acc_left[y-1]] % MOD)) % MOD;
        ans.push_back(modpow2(x, acc));
    }
    rep(i, Q) cout << ans[i] << endl;
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 30
memory_kb: 26524
final_rank: 14

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;

const int MAX = 1000000;
int MOD;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

//Nの剰余でのpow
ll modpow(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow(n, k-1)*n % MOD;
    }
    else{
        ll temp = modpow(n, k/2);
        return temp*temp % MOD;
    }
}

//N+1の剰余でのpow
ll modpow2(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow2(n, k-1)*n % (MOD+1);
    }
    else{
        ll temp = modpow2(n, k/2);
        return temp*temp % (MOD+1);
    }
}

//これを使うときにはCOMinit();を忘れずに

int main(void){
    int N, M, Q; cin >> N >> M >> Q;
    MOD = N;
    COMinit();
    vector<ll> Ainv(M);
    rep(i, M){
        ll a; cin >> a;
        Ainv[i] = inv[a];
    }
    vector<ll> acc_left(M*2+1, 1);
    for(int i = 1; i <= M*2; i++){
        acc_left[i] = acc_left[i-1]*Ainv[(i-1) % M] % MOD;
    } 
    vector<ll> ans;
    rep(i, Q){
        ll x, y, z; cin >> x >> y >> z;
        //Ainv[y-1]*Ainv[y]*...*Ainv[y-1+part-1]を計算
        ll whole = (z - (z % M)) / M; //巻物を完全に周回する回数
        ll part = z - whole*M; //端数
        //ll acc = modpow(acc_left[M], whole);
        ll acc = 1;
        for(int i = y-1, cnt = 0; cnt < z; i = (i+1) % M, cnt++) acc = acc*Ainv[i] % M;
        //acc = (acc * (acc_left[y-1+part] * inv[acc_left[y-1]] % MOD)) % MOD;
        ans.push_back(modpow2(x, acc));
    }
    rep(i, Q) cout << ans[i] << endl;
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 30
memory_kb: 26620
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)

typedef long long ll;

const int MAX = 1000000;
int MOD;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

ll modpow(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow(n, k-1)*n % MOD;
    }
    else{
        ll temp = modpow(n, k/2);
        return temp*temp % MOD;
    }
}

ll modpow2(ll n, ll k){
    if(k == 0) return 1;
    if(k%2){
        return modpow2(n, k-1)*n % (MOD+1);
    }
    else{
        ll temp = modpow2(n, k/2);
        return temp*temp % (MOD+1);
    }
}

//これを使うときにはCOMinit();を忘れずに

int main(void){
    int N, M, Q; cin >> N >> M >> Q;
    MOD = N;
    COMinit();
    vector<ll> Ainv(M);
    rep(i, M){
        ll a; cin >> a;
        Ainv[i] = inv[a];
    }
    vector<ll> acc_left(M*2+1, 1);
    for(int i = 1; i <= M*2; i++){
        acc_left[i] = acc_left[i-1]*Ainv[(i-1) % M] % MOD;
    } 
    vector<ll> ans;
    rep(i, Q){
        ll x, y, z; cin >> x >> y >> z;
        //Ainv[y-1]*Ainv[y]*...*Ainv[y+z-2]を計算
        ll whole = (z - (z % M)) / M; //巻物を完全に周回する回数
        ll part = z - whole*M; //端数
        ll acc = modpow(acc_left[M], whole);
        acc = (acc * (acc_left[y-1+part] * inv[acc_left[y-1]] % MOD)) % MOD;
        ans.push_back(modpow2(x, acc));
    }
    rep(i, Q) cout << ans[i] << endl;
    return 0;
}
accuracy: 0.034482758620689655
time_ms: 20
memory_kb: 26776
final_rank: 13

code_snippet: #include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <set>
#include <iomanip>
#include <queue>
#include <string>
#include <map>
#include <fstream>
#include <cassert>
#include <stack>
#include <climits>
#include <array>
#include <unordered_set>
#include <unordered_map>
#include <memory>
#include <functional>
#include <cfloat>
#include <numeric>
long long int power(const long long int base, const int exp, const long long int mod) {
	switch (exp) {
	case 0: return 1;
	case 1: return base % mod;
	default: return power(base * base % mod, exp >> 1, mod) * power(base, exp & 1, mod) % mod;
	}
}
std::pair<long long int, long long int> gcd_pair(const long long int a, const long long int b) {
	long long int ax{ 1 }, bx{ 0 }, ay{ 0 }, by{ 1 };
	while (a * ay + b * by != 0) {
		const auto n = a * ax + b * bx;
		const auto m = a * ay + b * by;
		const auto q = n / m;
		const auto ar = (ax - q * ay) % b;
		const auto br = (bx - q * by) % a;
		ax = ay;
		bx = by;
		ay = ar;
		by = br;
	}
	if (ax < 0) {
		ax += b;
		bx -= a;
	}
	return std::make_pair(ax, by);
}
long long int inverse(const long long int base, const long long int mod) {
	return gcd_pair(base, mod).first;
}
int main() {
	int n, m, q; std::cin >> n >> m >> q;
	std::vector<long long int> keys(m), product{ 1 };
	for (auto& d : keys) std::cin >> d;
	for (const auto d : keys) {
		product.push_back(product.back() * d % n);
	}
	for (const auto d : keys) {
		product.push_back(product.back() * d % n);
	}
	const auto all_key = std::accumulate(keys.begin(), keys.end(), 1LL, [n](const long long int acc, const long long int d) {return d * acc % n; });
	for (auto i = 0; i < q; ++i) {
		int x, y, z; std::cin >> x >> y >> z; --y;
		const auto key = power(all_key, z / m, n) * product[y + z % m] % n * inverse(product[y], n) % n;
		std::cout << power(x, inverse(key, n), n + 1) << '\n';
	}
}
accuracy: 1
time_ms: 140
memory_kb: 4508
final_rank: 8

code_snippet: #include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

class mint {
public:
    ll x;
    static ll mod;
    static unsigned long long mod_plus;
    static bool prime;

    static void set_mod(ll _mod, bool _prime = false) {
        mint::mod = _mod;
        mint::mod_plus = (LLONG_MAX / mod) * mod;
        mint::prime = _prime;
    }

    mint() { x = 0; }

    mint(ll _x) : x(_x < 0 ? ((_x += mod_plus) < 0 ? _x + mod_plus : _x) : _x){
        if(x >= mod) x %= mod;
    }

    mint operator-() {
        return x == 0 ? 0 : mod - x;
    }

    mint &operator+=(const mint &a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }

    mint operator+(const mint &a) const {
        return mint(*this) += a;
    }

    mint &operator-=(const mint &a) {
        if ((x -= a.x) < 0) x += mod;
        return *this;
    }

    mint operator-(const mint &a) const {
        return mint(*this) -= a;
    }

    mint &operator*=(const mint &a) {
        (x *= a.x) %= mod;
        return *this;
    }

    mint operator*(const mint &a) const {
        return mint(*this) *= a;
    }

    mint pow(unsigned long long pw) const {
        mint res(1), comp(*this);
        while (pw) {
            if (pw & 1) res *= comp;
            comp *= comp;
            pw >>= 1;
        }
        return res;
    }

    //modと互いに素な数aなら可能
    mint inv() const {
        if(prime){
            return mint(*this).pow(mod - 2);
        }else {
            // return p = (s.x, s.y)  :  a * p.fi + b * p.se = gcd(a, b);
            // 不要なのでxは省略
            ll su = mod, sy = 0, tu = x, ty = 1;
            while (tu != 0) {
                ll temp = su / tu;
                su -= tu * temp;
                sy -= ty * temp;
                swap(su, tu);
                swap(sy, ty);
            }
            return sy;
        }
    }

    mint &operator/=(const mint &a) {
        (x *= a.inv().x) %= mod;
        return *this;
    }

    mint operator/(const mint &a) const {
        mint res(*this);
        return res /= a;
    }
};
ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
long long mint::mod;
unsigned long long mint::mod_plus;
bool mint::prime;
using vm = vector<mint>;

mint find_mul(int y, int z, vm &d){
    mint res(1);
    res *= mint(d[M]).pow(z / M);
    z %= M;
    res *= d[y + z - 1];
    res /= d[y - 1];
    return res;
}

ll temp_pow(int x, mint _pw){
    ll pw = _pw.x, res(1), dbl(x);
    while(pw){
        if(pw&1) (res *= dbl) %= N + 1;
        (dbl *= dbl) %= N + 1;
        pw >>= 1;
    }
    return res;
}

int main() {
    cin>>N>>M>>Q;
    mint::set_mod(N, false);
    vm d(M + 1, 1);
    rep(i, M) cin>>d[i + 1].x;
    rep(i, M) d.push_back(d[i + 1]);
    rep(i, M * 2) d[i + 1] *= d[i];
    rep(_, Q){
        int x, y, z;
        cin>>x>>y>>z;
        mint mul = find_mul(y, z, d);
        cout<<temp_pow(x, mul.inv())<<endl;
    }
}
accuracy: 1
time_ms: 130
memory_kb: 3600
final_rank: 6