#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;
#define REP(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define FORR(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define CHMIN(a,b) (a) = min((a),(b))
#define CHMAX(a,b) (a) = max((a),(b))

int main()
{
    int n,p;
    while(1==1)
    {
        scanf("%d %d\n",&n,&p);
        if(n==0&&p==0)return 0;
        int ko[55]={};
        int zan=p;
        int poi=0;
        int judge=0;
        while(judge==0)
        {
            for(int i=0;i<n;i++)ko[i]+=zan/n;
            zan-=(zan/n)*n;
            while(zan>0)
            {
                ko[poi]++;
                if(ko[poi]==p)
                {
                    printf("%d\n",poi);
                    judge=1;
                    break;
                }
                zan--;
                poi++;
                if(poi>=n)poi-=n;
            }
            if(judge==1)break;
            while(zan==0)
            {
                if(ko[poi]>0)
                {
                    zan=ko[poi];
                    ko[poi]=0;
                }
                poi++;
                if(poi>=n)poi-=n;
            }
        }
    }
    return 0;
}

#include<bits/stdc++.h>

using namespace std;
const int mod = 1e9+7;
int ad(int x,int y) { x+=y; return x>=mod?x-mod:x; }
int sb(int x,int y) { x-=y; return x<0?x+mod:x; }
int mu(int x,int y) { return 1ll*x*y%mod; }
int ksm(int a,int b) {
	int ans = 1;
	for(;b;b>>=1,a=mu(a,a))
		if(b&1) ans = mu(ans,a);
	return ans;
}
int fac[4000005],piv[4000005],n,k;
int GC(int a,int b) {
	if(a<b) return 0;
	return mu(fac[a],mu(piv[b],piv[a-b]));
}
int f[2005][2005];
int main() {
	scanf("%d%d",&n,&k);
	if(k==1) {
		puts("1"); return 0;
	}
	fac[0] = 1;
	for(int i=1;i<=n*k;i++) fac[i] = mu(fac[i-1],i);
	piv[n*k] = ksm(fac[n*k],mod-2);
	for(int i=n*k-1;i>=0;i--) piv[i] = mu(piv[i+1],i+1);
	f[0][0] = 1;
	for(int i=1;i<=n;i++) {
		f[i][0] = 1;
		for(int j=1;j<=i;j++) {
			f[i][j] = ad(f[i-1][j],mu(GC((n-i)+(n-j+1)*(k-1)-1,k-2),mu(f[i][j-1],n-j+1)));
		}
	}
	printf("%d",f[n][n]);
}