#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef string str;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef pair<db,db> pd;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<db> vd;
typedef vector<str> vs;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<pd> vpd;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) (x).rbegin(), (x).rend()
#define rsz resize
#define ins insert
#define ft front()
#define bk back()
#define pf push_front
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7; // 998244353;
const int MX = 2e5+5;
const ll INF = 1e18;
const ld PI = acos((ld)-1);
const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1};
template<class T> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; }
int pct(int x) { return __builtin_popcount(x); }
int bit(int x) { return 31-__builtin_clz(x); } // floor(log2(x))
int cdiv(int a, int b) { return a/b+!(a<0||a%b == 0); } // division of a by b rounded up, assumes b > 0
// INPUT
template<class A> void re(complex<A>& c);
template<class A, class B> void re(pair<A,B>& p);
template<class A> void re(vector<A>& v);
template<class A, size_t SZ> void re(array<A,SZ>& a);
template<class T> void re(T& x) { cin >> x; }
void re(db& d) { str t; re(t); d = stod(t); }
void re(ld& d) { str t; re(t); d = stold(t); }
template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); }
template<class A> void re(complex<A>& c) { A a,b; re(a,b); c = {a,b}; }
template<class A, class B> void re(pair<A,B>& p) { re(p.f,p.s); }
template<class A> void re(vector<A>& x) { trav(a,x) re(a); }
template<class A, size_t SZ> void re(array<A,SZ>& x) { trav(a,x) re(a); }
// TO_STRING
#define ts to_string
template<class A, class B> str ts(pair<A,B> p);
template<class A> str ts(complex<A> c) { return ts(mp(c.real(),c.imag())); }
str ts(bool b) { return b ? "true" : "false"; }
str ts(char c) { str s = ""; s += c; return s; }
str ts(str s) { return s; }
str ts(const char* s) { return (str)s; }
str ts(vector<bool> v) {
bool fst = 1; str res = "{";
F0R(i,sz(v)) {
if (!fst) res += ", ";
fst = 0; res += ts(v[i]);
}
res += "}"; return res;
}
template<size_t SZ> str ts(bitset<SZ> b) {
str res = ""; F0R(i,SZ) res += char('0'+b[i]);
return res; }
template<class T> str ts(T v) {
bool fst = 1; str res = "{";
for (const auto& x: v) {
if (!fst) res += ", ";
fst = 0; res += ts(x);
}
res += "}"; return res;
}
template<class A, class B> str ts(pair<A,B> p) {
return "("+ts(p.f)+", "+ts(p.s)+")"; }
// OUTPUT
template<class A> void pr(A x) { cout << ts(x); }
template<class H, class... T> void pr(const H& h, const T&... t) {
pr(h); pr(t...); }
void ps() { pr("\n"); } // print w/ spaces
template<class H, class... T> void ps(const H& h, const T&... t) {
pr(h); if (sizeof...(t)) pr(" "); ps(t...); }
// DEBUG
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << to_string(h); if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "[" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 42
#endif
// FILE I/O
void setIn(string s) { freopen(s.c_str(),"r",stdin); }
void setOut(string s) { freopen(s.c_str(),"w",stdout); }
void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); }
void setIO(string s = "") {
unsyncIO();
// cin.exceptions(cin.failbit);
// throws exception when do smth illegal
// ex. try to read letter into int
if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
}
mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count());
/**
* Description: modular arithmetic operations
* Source:
* KACTL
* https://c...content-available-to-author-only...s.com/blog/entry/63903
* https://c...content-available-to-author-only...s.com/contest/1261/submission/65632855 (tourist)
* https://c...content-available-to-author-only...s.com/contest/1264/submission/66344993 (ksun)
* Verification:
* https://o...content-available-to-author-only...s.com/problems/modulararithmetic
*/
struct mi {
typedef decay<decltype(MOD)>::type T;
/// don't silently convert to T
T v; explicit operator T() const { return v; }
mi() { v = 0; }
mi(ll _v) {
v = (-MOD < _v && _v < MOD) ? _v : _v % MOD;
if (v < 0) v += MOD;
}
friend bool operator==(const mi& a, const mi& b) {
return a.v == b.v; }
friend bool operator!=(const mi& a, const mi& b) {
return !(a == b); }
friend bool operator<(const mi& a, const mi& b) {
return a.v < b.v; }
friend void re(mi& a) { ll x; re(x); a = mi(x); }
friend str ts(mi a) { return ts(a.v); }
mi& operator+=(const mi& m) {
if ((v += m.v) >= MOD) v -= MOD;
return *this; }
mi& operator-=(const mi& m) {
if ((v -= m.v) < 0) v += MOD;
return *this; }
mi& operator*=(const mi& m) {
v = (ll)v*m.v%MOD; return *this; }
mi& operator/=(const mi& m) { return (*this) *= inv(m); }
friend mi pow(mi a, ll p) {
mi ans = 1; assert(p >= 0);
for (; p; p /= 2, a *= a) if (p&1) ans *= a;
return ans;
}
friend mi inv(const mi& a) { assert(a.v != 0);
return pow(a,MOD-2); }
mi operator-() const { return mi(-v); }
mi& operator++() { return *this += 1; }
mi& operator--() { return *this -= 1; }
friend mi operator+(mi a, const mi& b) { return a += b; }
friend mi operator-(mi a, const mi& b) { return a -= b; }
friend mi operator*(mi a, const mi& b) { return a *= b; }
friend mi operator/(mi a, const mi& b) { return a /= b; }
};
typedef vector<mi> vmi;
typedef pair<mi,mi> pmi;
typedef vector<pmi> vpmi;
vector<vmi> comb;
void genComb(int SZ) {
comb.assign(SZ,vmi(SZ)); comb[0][0] = 1;
FOR(i,1,SZ) F0R(j,i+1)
comb[i][j] = comb[i-1][j]+(j?comb[i-1][j-1]:0);
}
mi ans, i2 = mi(1)/2, i3 = mi(1)/3;
int n,m,k;
pi r[MX], c[MX];
mi c2(mi x) { return x*(x-1)*i2; }
void vert() {
mi sum = c2(m);
map<int,int> cur;
cur[0] = 0, cur[m+1] = m+1;
int lst = 0;
vector<array<int,3>> ev;
F0R(i,k) {
ev.pb({r[i].f,1,i});
ev.pb({r[i].s+1,-1,i});
}
auto gap = [&](pi a, pi b) {
assert(a.s < b.f);
return c2(b.f-a.s-1);
};
auto ad = [&](int a, int b) {
auto it = cur.insert({a,b}).f;
sum -= gap(*prev(it),*next(it));
sum += gap(*prev(it),*it);
sum += gap(*it,*next(it));
};
auto del = [&](int a, int b) {
auto it = cur.find(a);
sum += gap(*prev(it),*next(it));
sum -= gap(*prev(it),*it);
sum -= gap(*it,*next(it));
cur.erase(it);
};
sort(all(ev));
trav(t,ev) {
ans += (t[0]-1-lst)*sum; lst = t[0]-1;
if (t[1] == 1) {
pi y = c[t[2]];
ad(y.f,y.s);
} else {
pi y = c[t[2]];
del(y.f,y.s);
}
// dbg("HUH",t,sum);
}
ans += (n-lst)*sum;
}
typedef array<mi,3> T;
T operator-=(T& a, T b) {
F0R(i,3) a[i] -= b[i];
return a;
}
T operator-(T a, T b) { return a -= b; }
T operator+=(T& a, T b) {
F0R(i,3) a[i] += b[i];
return a;
}
mi cum(T cval, mi r) {
return r*(cval[0]+(r+1)*i2*(cval[1]+cval[2]*(2*r+1)*i3));
}
mi cum(T cval, int l, int r) {
return cum(cval,r)-cum(cval,l-1);
}
int tim;
int lo(int a) {
if (a == k) {
return -MOD;
}
if (a == k+1) { // (0,0) -> (0,m+1) -> (n+1,m+1)
return min(m+1,tim);
}
assert(tim >= r[a].f+c[a].f);
return max(c[a].f,tim-r[a].s);
}
T LO(int a) {
if (a == k) return {-MOD,0,0};
if (a == k+1) {
if (tim >= m+1) return {m+1,0,0};
return {0,1,0};
}
assert(tim >= r[a].f+c[a].f);
if (tim >= r[a].s+c[a].f) return {-r[a].s,1,0};
return {c[a].f,0,0};
}
int hi(int a) {
if (a == k) { // (0,0) -> (n+1,0) -> (n+1,m+1)
return max(0,tim-(n+1));
}
if (a == k+1) {
return MOD;
}
assert(tim >= r[a].f+c[a].f);
return min(c[a].s,tim-r[a].f);
}
T HI(int a) {
if (a == k) {
if (tim >= n+1) return {-(n+1),1,0};
return {0,0,0};
}
if (a == k+1) return {MOD,0,0};
assert(tim >= r[a].f+c[a].f);
if (tim >= r[a].f+c[a].s) return {c[a].s,0,0};
return {-r[a].f,1,0};
}
struct cmp {
bool operator()(const int& a, const int& b) const {
return lo(a) < lo(b);
}
};
T stor[MX], cval;
void calc(int a, int b) {
auto x = LO(b)-HI(a);
assert(x[2] == 0); x[0] -= 1;
mi A = x[1], B = x[0];
x = {B*(B-1),A*(2*B-1),A*A};
F0R(i,3) x[i] *= i2;
// dbg("AD",a,b);
stor[a] = x; cval += x;
}
void diag() {
cval = {0,0,0}; tim = 1; calc(k,k+1);
// dbg("HUH",cval,cum(cval,2,2));
vector<array<int,3>> ev;
F0R(i,k) {
pi R = r[i], C = c[i];
ev.pb({R.s+C.s+1,-1,i});
ev.pb({R.f+C.f,0,i});
ev.pb({R.s+C.f,1,i});
ev.pb({R.f+C.s,1,i});
}
/*FOR(i,4,100) {
tim = i;
mi x = cum(LO(0),tim,tim), y = cum(HI(0),tim,tim);
assert(x == lo(0) && y == hi(0));
}
FOR(i,1,100) {
tim = i;
mi x = cum(LO(k),tim,tim), y = cum(HI(k),tim,tim);
assert(x == lo(k) && y == hi(k));
}
FOR(i,1,100) {
tim = i;
mi x = cum(LO(k+1),tim,tim), y = cum(HI(k+1),tim,tim);
assert(x == lo(k+1) && y == hi(k+1));
}
exit(0);*/
ev.pb({n+1,1,k}); ev.pb({m+1,2,k+1});
sort(all(ev));
set<int,cmp> cur; tim = 0;
cur.insert(k), cur.insert(k+1);
int lst = 1;
trav(t,ev) {
/*dbg(cur,ans);
dbg(lst+1,t[0]-1,cum(cval,lst+1,t[0]-1));
dbg(cval);
dbg(t);
dbg();*/
ans += cum(cval,lst+1,t[0]-1); lst = t[0]-1;
tim = t[0];
dbg(tim,HI(k),LO(k+1));
if (t[1] == -1) {
tim = lst;
auto it = cur.find(t[2]); assert(it != end(cur) && *it == t[2]);
cval -= stor[*prev(it)]; cval -= stor[*it];
dbg("SUB",*prev(it)); dbg("SUB",*it);
calc(*prev(it),*next(it));
cur.erase(it);
} else if (t[1] == 0) {
auto it = cur.insert(t[2]).f;
cval -= stor[*prev(it)]; dbg("SUB",*prev(it));
calc(*prev(it),*it); calc(*it,*next(it));
} else {
auto it = cur.find(t[2]); assert(it != end(cur) && *it == t[2]);
if (it != begin(cur)) {
cval -= stor[*prev(it)]; dbg("SUB",*prev(it));
calc(*prev(it),*it);
}
if (next(it) != end(cur)) {
cval -= stor[*it]; dbg("SUB",*it);
calc(*it,*next(it));
}
}
}
//dbg("OH",cur,cval);
ans += cum(cval,lst+1,n+m);
dbg(ans);
// exit(0);
}
int main() {
setIO(); re(n,m,k);
F0R(i,k) re(r[i].f,c[i].f,r[i].s,c[i].s);
diag();
dbg(ans);
F0R(i,k) r[i] = {n+1-r[i].s,n+1-r[i].f};
diag();
//dbg(ans);
//exit(0);
vert();
//dbg("HUH",ans);
swap(n,m);
F0R(i,k) swap(r[i],c[i]);
vert();
//dbg("HUH",ans);
ps(ans);
// you should actually read the stuff at the bottom
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiAKdHlwZWRlZiBsb25nIGxvbmcgbGw7CnR5cGVkZWYgbG9uZyBkb3VibGUgbGQ7CnR5cGVkZWYgZG91YmxlIGRiOyAKdHlwZWRlZiBzdHJpbmcgc3RyOyAKCnR5cGVkZWYgcGFpcjxpbnQsaW50PiBwaTsKdHlwZWRlZiBwYWlyP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