Problem:
Score : 600 points

Problem Statement

There is an arithmetic progression with L terms: s_0, s_1, s_2, ... , s_{L-1}.

The initial term is A, and the common difference is B. That is, s_i = A + B \times i holds.

Consider the integer obtained by concatenating the terms written in base ten without leading zeros. For example, the sequence 3, 7, 11, 15, 19 would be concatenated into 37111519. What is the remainder when that integer is divided by M?

Constraints

All values in input are integers.

1 \leq L, A, B < 10^{18}

2 \leq M \leq 10^9

All terms in the arithmetic progression are less than 10^{18}.

Input

Input is given from Standard Input in the following format:

L A B M

Output

Print the remainder when the integer obtained by concatenating the terms is divided by M.

Sample Input 1

5 3 4 10007

Sample Output 1

5563

Our arithmetic progression is 3, 7, 11, 15, 19, so the answer is 37111519 mod 10007, that is, 5563.

Sample Input 2

4 8 1 1000000

Sample Output 2

891011

Sample Input 3

107 10000000000007 1000000000000007 998244353

Sample Output 3

39122908

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
//#pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
//#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v) REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")
template <typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template <typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
ll MOD = 1e9 + 7;
constexpr int MAX = 10050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;
using ull = uint64_t;

struct Matrix {
	ll m;//size of matrix
	vector<ll> dp;
	vector<vector<ll>> mt;

	//constructor
	Matrix(ll x) :dp(x), mt(x, vector<ll>(x, 0)) { m = x; }

	vector<ll> matmul() {
		vector<ll> ret(m, 0);
		REP(i, m)REP(j, m) {
			ret[i] += mt[i][j] * dp[j] % MOD;
			ret[i] %= MOD;
		}
		return ret;
	}
	void update() {
		vector<vector<ll>> ret(m, vector<ll>(m, 0));
		REP(i, m)REP(j, m)REP(k, m) {
			ret[i][j] += mt[i][k] * mt[k][j] % MOD;
			ret[i][j] %= MOD;
		}
		mt = ret;
	}
	void matpow(ll k) {
		while (k) {
			if (k & 1)dp = matmul();
			update();
			k /= 2;
		}
	}
};

int cntd(int x) {
	int ret = 0;
	while (x) {
		ret++;
		x /= 10;
	}
	return ret;
}
int p10(int k) {
	int ret = 1;
	REP(_, k) {
		ret *= 10;
		ret %= MOD;
	}
	return ret;
}
int L, A, B, M;

void test() {
	M = 1e9 + 7;
	L = mt() % (int)10 + 1;
	A = mt() % (int)1e18 + 1;
	B = mt() % (int)1e18 + 1;
	MOD = M;
	cerr << L << " " << A << " " << B << " " << M << endl;
	int now = A;
	int ans = 0;
	REP(_, L) {
		int d = cntd(now);
		ans *= p10(d)%MOD;
		ans %= MOD;
		ans += now;
		now += B;
	}
	ans %= MOD;

	int lasd = cntd(A + B * L);
	vector<int>ks;
	int MAX_i = (1e18 - A + B - 1) / B;
	FOR(d, 2, lasd + 1) {
		int ng = 0, ok = MAX_i;
		while (abs(ok - ng) > 1) {
			int mid = (ok + ng) / 2;
			if (cntd(A + B * mid) >= d)ok = mid;
			else ng = mid;
		}
		ks.push_back(ok);
	}
	int pos = 0;

	int n = ks.size();
	int c = 0, s = A%MOD;
	REP(i, n) {
		//DEBUG(i);
		//DEBUG(ks[i]);
		Matrix mat(3);
		mat.mt[0][0] = p10(i + 1) % MOD;
		mat.mt[0][1] = 1;
		mat.mt[0][2] = 0;
		mat.mt[1][0] = 0;
		mat.mt[1][1] = 1;
		mat.mt[1][2] = B % MOD;
		mat.mt[2][0] = 0;
		mat.mt[2][1] = 0;
		mat.mt[2][2] = 1;
		mat.dp[0] = c;
		mat.dp[1] = s;
		mat.dp[2] = 1;
		mat.matpow(ks[i] - pos);
		pos = ks[i];
		c = mat.dp[0], s = mat.dp[1];
	}
	//DEBUG(lasd);

	Matrix mat(3);
	mat.mt[0][0] = p10(lasd) % MOD;
	mat.mt[0][1] = 1;
	mat.mt[0][2] = 0;
	mat.mt[1][0] = 0;
	mat.mt[1][1] = 1;
	mat.mt[1][2] = B % MOD;
	mat.mt[2][0] = 0;
	mat.mt[2][1] = 0;
	mat.mt[2][2] = 1;
	mat.dp[0] = c%MOD;
	mat.dp[1] = s%MOD;
	mat.dp[2] = 1;
	mat.matpow(L - pos);
	int res = mat.dp[0] % MOD;
	if (ans != res) {
		DEBUG(L);
		DEBUG(A);
		DEBUG(B);
		DEBUG(ans);
		DEBUG(res);
		exit(0);
	}
	else cerr << "ok" << endl;
}

void solve() {
	cin >> L >> A >> B >> M;
	MOD = M;
	int lasd = cntd(A + B * L);
	vector<int>ks;
	int MAX_i = (1e18 - A+B-1) / B+1;
	FOR(d, 2, lasd + 1) {
		int ng = 0, ok = MAX_i;
		while (abs(ok - ng) > 1) {
			int mid = (ok + ng) / 2;
			if (cntd(A + B * mid) >= d)ok = mid;
			else ng = mid;
		}
		ks.push_back(ok);
	}
	int pos = 0;

	int n = ks.size();
	int c = 0, s = A%MOD;
	REP(i, n) {
		//DEBUG(i);
		//DEBUG(ks[i]);
		Matrix mat(3);
		mat.mt[0][0] = p10(i + 1)%MOD;
		mat.mt[0][1] = 1;
		mat.mt[0][2] = 0;
		mat.mt[1][0] = 0;
		mat.mt[1][1] = 1;
		mat.mt[1][2] = B%MOD;
		mat.mt[2][0] = 0;
		mat.mt[2][1] = 0;
		mat.mt[2][2] = 1;
		mat.dp[0] = c%MOD;
		mat.dp[1] = s%MOD;
		mat.dp[2] = 1;
		mat.matpow(ks[i] - pos);
		pos = ks[i];
		c = mat.dp[0]%MOD, s = mat.dp[1]%MOD;
	}
	//DEBUG(lasd);

	Matrix mat(3);
	mat.mt[0][0] = p10(lasd) % MOD;
	mat.mt[0][1] = 1;
	mat.mt[0][2] = 0;
	mat.mt[1][0] = 0;
	mat.mt[1][1] = 1;
	mat.mt[1][2] = B % MOD;
	mat.mt[2][0] = 0;
	mat.mt[2][1] = 0;
	mat.mt[2][2] = 1;
	mat.dp[0] = c%MOD;
	mat.dp[1] = s%MOD;
	mat.dp[2] = 1;
	mat.matpow(L - pos);
	print(mat.dp[0] % MOD);
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	//while (true)test();
	//int q;
	//cin >> q;
	//while (q--)
	solve();
}