Language: C++

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using P = pair<int, int>;
#define debug(var) cerr << "[" << #var << "] " << var << endl
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)

const int dx[] = {-1,0,1,0};
const int dy[] = {0,-1,0,1};
ll mod;

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 Matrix {
    mint m[3][3];
    Matrix(vector<mint> v) {
        rep(i, 9) {
            m[i/3][i%3] = v[i];
        }
    }
};

Matrix multiple(Matrix a, Matrix b) {
    vector<mint> v(9);
    rep(i, 9) {
        ll x = i/3;
        ll y = i%3;
        rep(j, 3) {
            v[i] += a.m[x][j]*b.m[j][y];
        }
    }
    return Matrix(v);
}

Matrix power(Matrix a, ll n) {
    if (n==0) {
        vector<mint> v(9);
        rep(i, 9) {
            if (i==0 || i==4 || i==8) v[i] = 1;
            else v[i] = 0;
        }
        return Matrix(v);
    }
    if (n==1) return a;
    if (n%2==0) {
        return power(multiple(a, a), n/2);
    }
    return multiple(a, power(a, n-1));
}



int main(){
    ll l, a, b;
    cin >> l >> a >> b >> mod;
    if (l==1) {
        cout << a%mod << endl;
        return 0;
    }
    ll keta = 1;
    mint ans = 0;
    while (1) {
        ll il;
        ll ir;
        ir = ((pow(10LL, keta)-1)-a);
        if (ir<0) {
            keta++;
            continue;
        }
        else ir /= b;
        il = (pow(10LL, keta-1)-1-a);
        if (il<0) il = -1LL;
        else il /= b;
        ll num = ir - il;
        num = min(num, l);
        if (num==0) {
            keta++;
            continue;
        }
        ir = il + num;
        l -= num;

        //debug(il);
        //debug(ir);
        mint z = mint(10).pow(keta);
        vector<mint> initial = {z, 1, 0, 0, 1, mint(b), 0, 0, 1};

        Matrix matrix(initial);
        Matrix final = power(matrix, num);
        mint prev = ans;
        ans *= (final.m[0][0]);
        ans += final.m[0][1]*mint(a+b*(il+1));
        ans += final.m[0][2];
        mint xx = prev*final.m[1][0] + final.m[1][1]*mint(a+b*(il+1)) + final.m[1][2];
        mint yy = prev*filan.m[2][0] + final.m[2][1]*mint(a+b*(il+1)) + final.m[2][2];
        if (xx.x != mint(a+b*(ir+1)).x) {
            return -1;
        }
        if (yy.x!=1) {
            return -1;
        }
        //debug(ans);
        //ll res = final.m[0][0]*
        //ans = ans*(mint(10).pow(total)) + res;
        if (l==0) break;
        keta++;
    }
    
    
    cout << ans << endl;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:149:24: error: 'filan' was not declared in this scope
  149 |         mint yy = prev*filan.m[2][0] + final.m[2][1]*mint(a+b*(il+1)) + final.m[2][2];
      |                        ^~~~~