#include #include #include #include #include #include #include #include #include #include #include #include #define SZ(X) ((int)(X).size()) #define ALL(X) (X).begin(), (X).end() #define REP(I, N) for (int I = 0; I < (N); ++I) #define REPP(I, A, B) for (int I = (A); I < (B); ++I) #define RI(X) scanf("%d", &(X)) #define RII(X, Y) scanf("%d%d", &(X), &(Y)) #define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z)) #define DRI(X) int (X); scanf("%d", &X) #define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y) #define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z) #define RS(X) scanf("%s", (X)) #define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) #define MP make_pair #define PB push_back #define MS0(X) memset((X), 0, sizeof((X))) #define MS1(X) memset((X), -1, sizeof((X))) #define LEN(X) strlen(X) #define F first #define S second typedef long long LL; using namespace std; const int SIZE = 1e5+10; LL mypow(LL x,LL y,int mod){ x%=mod; LL res=1%mod; while(y){ if(y&1)res=res*x%mod; y>>=1; x=x*x%mod; } return res; } LL f(int x,int y1,int y2,int p){ LL now=1,an=0; int pn=0; REPP(i,1,x+1){ int me=(x+1-i); while(me%p==0){ me/=p; pn++; } now=now*me%p; me=i; while(me%p==0){ me/=p; pn--; } now=now*mypow(me,p-2,p)%p; if(y1<=i&&i<=y2&&!pn){ an=(an+now)%p; } } return an; } int main(){ int x1,y1,x2,y2,p; while(RII(x1,y1)==2){ RII(x2,y2); RI(p); LL an=f(x2+1,y1+1,y2+1,p)-f(x1,y1+1,y2+1,p); an%=p; if(an<0)an+=p; cout<