// suo #pragma GCC optimize("O3,unroll-loops") #include #include #include #include #include // #include "atcoder/segtree.hpp" // using namespace atcoder; using namespace std; using namespace __gnu_cxx; using namespace __gnu_pbds; typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; // typedef tree, null_type, greater>, rb_tree_tag, tree_order_statistics_node_update> ordered_set; template using ordered_multiset = tree, rb_tree_tag, tree_order_statistics_node_update>; typedef long long ll; typedef long double ld; using usize = size_t; using NAME = void; typedef pair pi; typedef pair pl; typedef pair pd; typedef vector vi; typedef vector vd; typedef vector vl; typedef vector vpi; typedef vector vpl; typedef vector vvi; typedef vector vvl; template using pq = priority_queue; template using pqg = priority_queue, greater>; // #define mp make_pair #define int long long #define uint long long #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define ins insert #define fi first #define se second #define lso(s) ((s) & -(s)) int lg(ll s) { return s ? __builtin_clzll(1) - __builtin_clzll(s) : -1; }//lg(1)=0, lg(2)=1, lg(3)=1, lg(4)=2, ... #define all(x) begin(x), end(x) // #define sz(x) (int)(x).sz() const ll MOD = (int)1e9 + 7; const ll mod = 998244353; const double EPS = 1e-16; const ll BIG = 1e18; const ll INF = 4e18; ll lcm(ll a,ll b) { return a/__gcd(a,b)*b; } ll floor(ll a, ll b) { return a / b - (a % b < 0); } ll ceil(ll a, ll b) { return a / b + (a % b > 0); } template istream& operator>>(istream& in, vector &vec){ for(auto &x : vec){ in >> x; } return in; } #ifdef sparkle template ostream& operator << (ostream &o, vector vec) { o << "{"; int f = 0; for (T i : vec) o << (f++ ? " " : "") << i; return o << "}"; } void bug__(int c, auto ...a) { cerr << "\e[1;" << c << "m"; (..., (cerr << a << " ")); cerr << "\e[0m" << endl; } #define bug_(c, x...) bug__(c, __LINE__, "[" + string(#x) + "]", x) #define bug(x...) bug_(32, x) #define bugv(x...) bug_(36, vector(x)) #define safe bug_(33, "safe") #else #define bug(x...) void(0) #define bugv(x...) void(0) #define safe void(0) #endif struct custom_hash { static uint64_t splitmix64(uint64_t x) { // http://xorshift.di.unimi.a/splitmix64.c x += 0x9e3779b97f4a7c15ULL; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL; x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL; return x ^ (x >> 31); } size_t operator()(uint64_t x) const noexcept { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } size_t operator()(const pair& pq) const noexcept { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); uint64_t h1 = splitmix64((uint64_t)pq.first + FIXED_RANDOM); uint64_t h2 = splitmix64((uint64_t)pq.second + FIXED_RANDOM + 0x9e3779b97f4a7c15ULL); return h1 ^ (h2 + 0x9e3779b97f4a7c15ULL + (h1 << 6) + (h1 >> 2)); } }; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll gen() { ll x = 0; while(x == 0) x = rng() % MOD; return x; } struct mint { ll x; mint(ll x=0):x((x%MOD+MOD)%MOD){} 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 b) const { mint res(1), a(*this); while (b) { if (b & 1) res *= a; a *= a; b >>= 1; } return res; } // for pq 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;} }; ostream& operator<<(ostream& os, const mint& a) {os << a.x; return os;} template inline void chadd(T &x, T y) { x += y; if(x >= mod) x -= mod; } template inline void chmin(T &x, T y) { if (y < x) x = y; } template inline void chmax(T &x, T y) { if (y > x) x = y; } auto mul(int a, int b) -> int { return (a % MOD * b % MOD) % MOD; } auto add(int a, int b) -> int { return (a % MOD + b % MOD) % MOD; } auto sub(int a, int b) -> int { return (a % MOD - b % MOD + MOD) % MOD; } inline NAME before() { } // #define tests auto sparkle() -> void { } auto fastexpo(int a, int b, int md) -> int { int res = 1; a %= md; while (b) { if (b & 1) { res = (res * a) % md; } b >>= 1; a = (a * a) % md; } return res; } [[gnu::target("avx2"), gnu::flatten]] auto main() -> int32_t { std::cin.tie(nullptr)->sync_with_stdio(0); std::cin.exceptions(std::ios::failbit | std::ios::badbit); // freopen("in","r",stdin); // freopen("outt","w",stdout); int TC = 1; // cin >> TC; while (TC --> 0) { int n, p, q; cin >> n >> p >> q; vector a(n); for (auto& i : a) cin >> i; vector cnt(p, 0); int ans = 0; for (int i = 2; i < n - 1; ++i) { int j = i - 1; for (int k = 0; k < j; ++k) { int sum = (fastexpo(10, a[k], p) + fastexpo(9, a[j], p)) % p; cnt[sum]++; } for (int l = i + 1; l < n; ++l) { int sum = (fastexpo(7, a[i], p) + fastexpo(5, a[l], p)) % p; int res = (q - sum + p) % p; ans += cnt[res]; } } cout << ans << '\n'; } return 0; }