#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define REP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME #define REP3(i, l, r, s) \ for (int i = int(l), rep3_r = int(r), rep3_s = int(s); i < rep3_r; \ i += rep3_s) #define REP2(i, l, r) REP3(i, l, r, 1) #define REP1(i, n) REP2(i, 0, n) #define rep(...) REP_OVERLOAD(__VA_ARGS__, REP3, REP2, REP1, )(__VA_ARGS__) #define repin(i, l, r) for (int i = int(l), repin_r = int(r); i <= repin_r; ++i) #define RREP_OVERLOAD(arg1, arg2, arg3, arg4, NAME, ...) NAME #define RREP3(i, l, r, s) \ for (int i = int(r) - 1, rrep3_l = int(l), rrep3_s = int(s); i >= rrep3_l; \ i -= rrep3_s) #define RREP2(i, l, r) RREP3(i, l, r, 1) #define RREP1(i, n) RREP2(i, 0, n) #define rrep(...) RREP_OVERLOAD(__VA_ARGS__, RREP3, RREP2, RREP1, )(__VA_ARGS__) #define rrepin(i, l, r) \ for (int i = int(r), rrepin_l = int(l); i >= rrepin_l; --i) #include #include #include namespace rklib { // a <- max(a, b) template bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; } // a <- min(a, b) template bool chmin(T &a, const T &b) { if (a > b) { a = b; return true; } return false; } // if a < 0: a <- b // else: a <- min(a, b) template bool chmin_non_negative(T &a, const T &b) { if (a < 0 || a > b) { a = b; return true; } return false; } // floor(num / den) template T div_floor(T num, T den) { if (den < 0) num = -num, den = -den; return num >= 0 ? num / den : (num + 1) / den - 1; } // ceil(num / den) template T div_ceil(T num, T den) { if (den < 0) num = -num, den = -den; return num <= 0 ? num / den : (num - 1) / den + 1; } namespace internal { template T remainder_count(T r, T b, T m) { return r / m * b + std::min(b, r % m); } } // namespace internal // Number of integer x s.t. // - x in [l, r) // - x mod m in [a, b) template T remainder_count(T l, T r, T a, T b, T m) { assert(l >= 0); assert(m >= 1); if (l >= r || a >= b) return 0; if (m <= a || b < 0) return 0; chmax(a, T(0)); chmin(b, m); auto res = internal::remainder_count(r, b, m); if (l >= 1) res -= internal::remainder_count(l, b, m); if (a >= 1) res -= internal::remainder_count(r, a, m); if (l >= 1 && a >= 1) res += internal::remainder_count(l, a, m); return res; } } // namespace rklib using namespace std; using namespace rklib; using lint = long long; using pii = pair; using pll = pair; #include using mint = atcoder::modint998244353; lint g(lint x) { return x * (x + 1) / 2; } lint f(lint l, lint r) { return g(r) - g(l - 1); } int main() { int h, w, K; cin >> h >> w >> K; mint ans = 0; rep(kk, K) { int x, y, v; cin >> x >> y >> v; lint tmp = 0; if (y - x + 1 >= 1) { tmp += f(1, x); } else { tmp += f(x - y + 1, x); } y = w - y + 1; if (y - x + 1 >= 1) { tmp += f(1, x); } else { tmp += f(x - y + 1, x); } tmp -= x; ans += mint(tmp) * v; } printf("%u\n", ans.val()); }