Problem p03016 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define mod 1000000007
#define mad(a, b) a = (a + b) % mod
#define Nmax 55

ll mo;

ll po(ll x, ll y) {
  if (y < 0)
    cout << 1 / 0 << endl;
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x;
    x = x * x;
  }
  return res;
}

ll pomo(ll x, ll y) {
  if (y < 0)
    cout << 1 / 0 << endl;
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x % mo;
    x = x * x % mo;
  }
  return res;
}

struct matrix {
  ll t[3][3];
};
matrix mul(matrix a, matrix b) {
  matrix c;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      ll res = 0;
      for (int k = 0; k < 3; k++) {
        res += a.t[i][k] * b.t[k][j];
      }
      c.t[i][j] = res % mo;
    }
  return c;
}
ll solve(ll d, ll a, ll b, ll l) {
  matrix r[66];
  ll kei[] = {1, b % mo, 0, 0, po(10, d) % mo, 0, 1, a % mo, 1};
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      r[0].t[i][j] = kei[i * 3 + j];
    }
  for (int i = 1; i < 62; i++)
    r[i] = mul(r[i - 1], r[i - 1]);
  matrix res;
  bool fi = 1;
  for (int i = 0; i < 62; i++) {
    if ((1LL << i) & l) {
      if (fi)
        res = r[i], fi = 0;
      else
        res = mul(res, r[i]);
    }
  }
  return res.t[2][1];
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  ll l, a, b;
  cin >> l >> a >> b >> mo;
  ll ans = 0;
  ll ruiketa = 1;
  for (int d = 18; d >= 1; d--) {
    if (po(10, d) <= a)
      continue;
    if (a + b * (l - 1) < po(10, d - 1))
      continue;
    ll s = a;
    if (s < po(10, d - 1))
      s = a + (po(10, d - 1) - 1 - a + b) / b * b;
    ll t = a + b * (l - 1);
    if (po(10, d) <= t)
      t = a + (po(10, d) - 1 - a) / b * b;
    ll len = (t - s) / b + 1;
    if (len > 0) {
      ll res = solve(d, s, b,
                     len); // cout<<d<<" "<<s<<" "<<b<<" "<<len<<" "<<res<<endl;
      ans = ans + ruiketa * res;
      ans %= mo;
      ruiketa = ruiketa * pomo(10, len * d) % mo;
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define mod 1000000007
#define mad(a, b) a = (a + b) % mod
#define Nmax 55

ll mo;

ll po(ll x, ll y) {
  if (y < 0)
    cout << 1 / 0 << endl;
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x;
    x = x * x;
  }
  return res;
}

ll pomo(ll x, ll y) {
  if (y < 0)
    cout << 1 / 0 << endl;
  ll res = 1;
  for (; y; y >>= 1) {
    if (y & 1)
      res = res * x % mo;
    x = x * x % mo;
  }
  return res;
}

struct matrix {
  ll t[3][3];
};
matrix mul(matrix a, matrix b) {
  matrix c;
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      ll res = 0;
      for (int k = 0; k < 3; k++) {
        res += a.t[i][k] * b.t[k][j];
      }
      c.t[i][j] = res % mo;
    }
  return c;
}
ll solve(ll d, ll a, ll b, ll l) {
  matrix r[66];
  ll kei[] = {1, b % mo, 0, 0, po(10, d) % mo, 0, 1, a % mo, 1};
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++) {
      r[0].t[i][j] = kei[i * 3 + j];
    }
  for (int i = 1; i < 62; i++)
    r[i] = mul(r[i - 1], r[i - 1]);
  matrix res;
  bool fi = 1;
  for (int i = 0; i < 62; i++) {
    if ((1LL << i) & l) {
      if (fi)
        res = r[i], fi = 0;
      else
        res = mul(res, r[i]);
    }
  }
  return res.t[2][1];
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  ll l, a, b;
  cin >> l >> a >> b >> mo;
  ll ans = 0;
  ll ruiketa = 1;
  for (int d = 18; d >= 1; d--) {
    if (po(10, d) <= a)
      continue;
    if (a + b * (l - 1) < po(10, d - 1))
      continue;
    ll s = a;
    if (s < po(10, d - 1))
      s = a + (po(10, d - 1) - 1 - a + b) / b * b;
    ll t = a + b * (l - 1);
    if (po(10, d) <= t)
      t = a + (po(10, d) - 1 - a) / b * b;
    ll len = (t - s) / b + 1;
    if (len > 0) {
      ll res = solve(d, s, b,
                     len); // cout<<d<<" "<<s<<" "<<b<<" "<<len<<" "<<res<<endl;
      ans = ans + ruiketa * res;
      ans %= mo;
      ruiketa = ruiketa * pomo(pomo(10, len), d) % mo;
    }
  }
  cout << ans << endl;
}