#pragma GCC optimize ("O3")
#pragma GCC target ("tune=native")
#pragma GCC target ("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i,n) for (long long i=0, i##_len=(n); i<i##_len; i++)
#define RANGE(i,is,ie) for (long long i=(is), i##_end=(ie); i<=i##_end; i++)
#define DSRNG(i,is,ie) for (long long i=(is), i##_end=(ie); i>=i##_end; i--)
#define UNIQUE(v) { sort((v).begin(), (v).end()); (v).erase(unique((v).begin(), (v).end()), (v).end()); }
template<class T> bool chmax(T &a, const T &b) {if (a < b) {a = b; return true;} return false; }
template<class T> bool chmin(T &a, const T &b) {if (a > b) {a = b; return true;} return false; }
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q) cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q) { cerr << "[DEBUG] " #q ": ["; REP(dumpall_i, (q).size()) { cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", "); } cerr << "] at " __FILE__ ":" << __LINE__ << endl; }
template<class T> void DUMPTABLE(const std::vector<std::vector<T>> &t) {
    size_t p = t.size(), q = t[0].size();
    std::cerr << "[DEBUG]: [\n";
    for (size_t i = 0; i < p; i++) {
        std::cerr << "    [";
        for (size_t j = 0; j < q; j++) {
            std::cerr << t[i][j] << (j == q-1 ? "" : ",");
        }
        std::cerr << "],\n";
    }
    std::cerr << "]\n";
}
template<class T> T gcd(const T &a, const T &b) { return a < b ? gcd(b, a) : b ? gcd(b, a % b) : a; }
template<class T> T lcm(const T &a, const T &b) { return a / gcd(a, b) * b; }

// gcc拡張マクロ
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using  ll = long long;
using ull = unsigned long long;
using  ld = long double;
using namespace std;

// モジュール
// 行列累乗; 1e9+7で割ったあまりを求めたい場合は適宜コメントアウトを解除
ll p;

// 単位行列; idtm<型>(大きさ)
template<typename T>
vector<vector<T>> idtm(const size_t n) {
    vector<vector<T>> r(n, vector<T>(n, 0));
    REP(i, n) r[i][i] = 1;
    return r;
}

// 行列の積
template<typename T>
vector<vector<T>> mltm(const vector<vector<T>> &a, const vector<vector<T>> &b) {
    ull m = a.size(), q = b.size(), r = b[0].size();
    vector<vector<T>> c(m, vector<T>(r, (T)0));
    REP(i, m) REP(j, r) {
        REP(k, m) {
            // c[i][j] += a[i][k] * b[k][j];
            c[i][j] += a[i][k] * b[k][j] % p; c[i][j] %= p;
        }
    }
    return c;
}

// 行列の累乗
template<typename T>
vector<vector<T>> powm(const vector<vector<T>> &a, const ll n) {
    if (n == 0) {
        return idtm<T>(a.size());
    } else if (n == 1) {
        return a;
    } else if (n % 2 == 0) {
        vector<vector<T>> t = powm(a, n/2);
        return mltm(t, t);
    } else {
        vector<vector<T>> t = powm(a, n/2);
        return mltm(mltm(t, t), a);
    }
}

vector<ll> powll10(19, 1);

// n桁に収まるのはs_(返り値)以下, n < 18厳守
ll dgpoint(ll a, ll b, ll l, ll n) {
    if (n >= 18) return l-1;
    ll ok = -1;
    ll ng = l;
    while (ng - ok > 1) {
        ll mid = (ok + ng) / 2;
        ll smid = a + b * mid;
        if (smid < powll10[n]) {
            ok = mid;
        } else {
            ng = mid;
        }
    }
    return ok;
}

// 処理内容
int main() {
    
    ll l, a, b, m; cin >> l >> a >> b >> m;

    REP(i, 19) {
        if (i) {
            powll10[i] = powll10[i-1] * 10;
        }
    }

    p = m;

    vector<ll> diglim(19, -1); // s_diglim[i]以下はi桁に収まる
    vector<ll> dnum(19, 0); // i桁に収まる数の個数
    ll d = 0; // 総桁数
    REP(i, 19) {
        diglim[i] = dgpoint(a, b, l, i);
    }
    ll prev = -1;
    RANGE(i, 1, 18) {
        if (diglim[i] >= 0) {
            dnum[i] = diglim[i] - prev;
            d += dnum[i] * i;
            prev = diglim[i];
        }
    }

    DUMPALL(diglim);
    DUMPALL(dnum);

    vector<vector<ll>> x0 = {{0}, {a%m}, {1}};

    DUMPTABLE(x0);

    RANGE(i, 1, 18) {

        ll n = dnum[i];
        if (!n) continue;
        
        ll pd = powll10[i];
        DUMP(pd);
        vector<vector<ll>> a = {
            {pd, 1, 0},
            { 0, 1, b},
            { 0, 0, 1}
        };
        for (auto &p: a) for (auto &v: p) v %= m;
        x0 = mltm(powm(a, n), x0);
        DUMPTABLE(x0);

    }

    cout << x0[0][0] << endl;

    
}

Measured memory_bytes: 256