传送门
每一位如果没有被限制,对答案的贡献会是 ( n × ( n + 1 ) 2 ) (\frac {n\times(n+1)}2) (2n×(n+1)?),如果被限制就减去所有限制的数的和,减的时候去重。
#include<bits/stdc++.h>
#define il inline
#define ll long long
using namespace std;
const int mo=1e9+7,N=1e5+5;
#define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
char buf[1<<21],*p1=buf,*p2=buf;
il int read(){int x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=(x+(x<<2)<<1)+c-48;return x*f;
}
struct A{ll x,y;}a[N];ll s[N];
il bool cmp(A a,A b){return a.x==b.x?a.y<b.y:a.x<b.x;}
ll n,m,k,ss,ct,ans;
il ll qpow(ll a,ll b){ll res=1;for(;b;b>>=1,a=a*a%mo)if(b&1)res=res*a%mo;return res;}
int main(){n=read(),m=read(),k=read(),ss=(n*(n+1)>>1)%mo;for(int i=1;i<=k;++i) a[i].x=read(),a[i].y=read();sort(a+1,a+k+1,cmp);for(int i=1;i<=k;++i){if(a[i].x!=a[i-1].x) s[++ct]=ss;else if(a[i].y==a[i-1].y) continue;s[ct]=(s[ct]-a[i].y+mo)%mo;}ans=qpow(ss,m-ct);for(int i=1;i<=ct;++i) (ans*=s[i])%=mo;return !printf("%lld",ans);
}