#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+6;

int n,q,tot;
int h[N],pos[N*4];
int ls[N*20],rs[N*20],siz[N*20];
void update(int &rt,int pre,int k,int val,int l,int r)
{
    rt=++tot;
    ls[rt]=ls[pre];
    rs[rt]=rs[pre];
    siz[rt]=siz[pre]+val;
    if(l==r)
        return ;
    int m=(l+r)>>1;
    if(k<=m)
        update(ls[rt],ls[pre],k,val,l,m);
    else
        update(rs[rt],rs[pre],k,val,m+1,r);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return siz[rt];
    }
    int m=(l+r)>>1,ans=0;
    if(m>=L)
        ans+=query(L,R,l,m,ls[rt]);
    if(m<R)
        ans+=query(L,R,m+1,r,rs[rt]);
    return ans;
}
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot=0;
        int k,d;
        cin>>n>>k>>d;

        for(int i=1; i<=n; i++)
        {
            int c;
            cin>>c;
            if(pos[c])
            {
                update(h[i],h[i-1],pos[c],-1,1,n);
                update(h[i],h[i],i,1,1,n);
            }
            else
                update(h[i],h[i-1],i,1,1,n);
            pos[c]=i;
            a[i]=c;
        }
        int ans=0x3f3f3f3f;
        for(int i=1; i<=n-d+1; i++)
        {
            ans=min(ans,query(i,i+d-1,1,n,h[i+d-1]));
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)h[i]=pos[a[i]]=0;
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=2e5+100;
int n,k,d;
int cnt[N*10],a[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int res=0;
        scanf("%d%d%d",&n,&k,&d);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=d;i++)
        {
            if(!cnt[a[i]])res++;
            cnt[a[i]]++;
        }
        int ans=res;
        for(int i=d+1;i<=n;i++)
        {
            if(!cnt[a[i]])res++;
            cnt[a[i]]++;
            cnt[a[i-d]]--;
            if(!cnt[a[i-d]])res--;
            ans=min(ans,res);
        }
        printf("%d\n",ans);
        for(int i=1;i<=k;i++)cnt[i]=0;
    }
    return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N=2e5+100;
int n,k,d;
int cnt[N*10],a[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int res=0;
        scanf("%d%d%d",&n,&k,&d);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=d;i++)
        {
            if(!cnt[a[i]])res++;
            cnt[a[i]]++;
        }
        int ans=res;
        for(int i=d+1;i<=n;i++)
        {
            if(!cnt[a[i]])res++;
            cnt[a[i]]++;
            cnt[a[i-d]]--;
            if(!cnt[a[i-d]])res--;
            ans=min(ans,res);
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)cnt[a[i]]=0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=2e5+6;

int n,q,tot;
int h[N],pos[N*4];
int ls[N*20],rs[N*20],siz[N*20];
void update(int &rt,int pre,int k,int val,int l,int r)
{
    rt=++tot;
    ls[rt]=ls[pre];
    rs[rt]=rs[pre];
    siz[rt]=siz[pre]+val;
    if(l==r)
        return ;
    int m=(l+r)>>1;
    if(k<=m)
        update(ls[rt],ls[pre],k,val,l,m);
    else
        update(rs[rt],rs[pre],k,val,m+1,r);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return siz[rt];
    }
    int m=(l+r)>>1,ans=0;
    if(m>=L)
        ans+=query(L,R,l,m,ls[rt]);
    if(m<R)
        ans+=query(L,R,m+1,r,rs[rt]);
    return ans;
}
int a[N];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        tot=0;
        int k,d;
        scanf("%d%d%d",&n,&k,&d);

        for(int i=1; i<=n; i++)
        {
            int c;
            scanf("%d",&c);
            if(pos[c])
            {
                update(h[i],h[i-1],pos[c],-1,1,n);
                update(h[i],h[i],i,1,1,n);
            }
            else
                update(h[i],h[i-1],i,1,1,n);
            pos[c]=i;
            a[i]=c;
        }
        int ans=0x3f3f3f3f;
        for(int i=1; i<=n-d+1; i++)
        {
            ans=min(ans,query(i,i+d-1,1,n,h[i+d-1]));
        }
        printf("%d\n",ans);
        for(int i=1;i<=n;i++)h[i]=pos[a[i]]=0;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int  main(int argc, char const *argv[])
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	int test;
	cin >> test;
	while( test-- ){
		int n,k,d;

		cin >> n >> k >> d ;
		int a[n];
		int hash[k+1] ={0};
		for (int i = 0; i < n; ++i)
			cin >> a[i] ;

		int flag = 0 , ans = 0 ;
		for (int i = 0 ; i <  d ; i ++)
		{
			if( hash[a[i]] == 0)
					ans ++;
			hash[a[i]] += 1 ;
		}
		//cout << "ans  " << ans  << '\n';
		int x = ans ;
		for (int i = d ; i < n ; ++i)
		{
			hash[a[i-d]]-- ;
			if( hash[a[i]] == 0 ){
				if( hash[a[i-d]] <= 0 )
					ans = ans;
				else
					ans += 1;
				hash[a[i]] += 1 ;
				//cout << " ++  " << a[i] << " "<< ans  << '\n';
			}
			else {
				if( a[i] == a[i-d] ){
					hash[a[i]] += 1;
					ans = ans;
				}
				else{
					if( hash[a[i-d]] >= 1)
						ans = ans;
					else
						ans -= 1;
					hash[a[i]] += 1;
				}
				//cout << " -- " << a[i] << "  "<< ans  << '\n';
			}

			x = min(x,ans);

		}
		cout << x << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int  main(int argc, char const *argv[])
{
	int test;
	cin >> test;
	while( test-- ){
		int n,k,d;

		cin >> n >> k >> d ;
		int a[n];int hash[k+1] ;
		for (int i = 0; i < n; ++i)
		{
			cin >> a[i];
		}
		int flag = 0 ,ans = 0 , x = 1e8;
		for (int i = 0; i < n - d + 1; ++i)
		{

			ans = 0 ; flag = 0;
			memset(hash,0,sizeof(hash));
			for (int j = i; j < i + d ; ++j)
			{
				flag =  1;
				if( hash[a[j]] == 0)
					ans ++;
				hash[a[j]] = 1 ;
			}
            if( flag == 1)
			   x = min(x,ans);
			//cout << " i "<< i <<" " << x << '\n';
		}
		cout << x << "\n";
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int  main(int argc, char const *argv[])
{
	int test;
	cin >> test;
	while( test-- ){
		int n,k,d;

		cin >> n >> k >> d ;
		int a[n];
		int hash[k+1] ={0};
		for (int i = 0; i < n; ++i)
			cin >> a[i] ;

		int flag = 0 , ans = 0 ;
		for (int i = 0 ; i <  d ; i ++)
		{
			if( hash[a[i]] == 0)
					ans ++;
			hash[a[i]] += 1 ;
		}
		//cout << "ans  " << ans  << '\n';
		int x = ans ;
		for (int i = d ; i < n ; ++i)
		{
			hash[a[i-d]]-- ;
			if( hash[a[i]] == 0 ){
				if( hash[a[i-d]] <= 0 )
					ans = ans;
				else
					ans += 1;
				hash[a[i]] += 1 ;
				//cout << " ++  " << a[i] << " "<< ans  << '\n';
			}
			else {
				if( a[i] == a[i-d] ){
					hash[a[i]] += 1;
					ans = ans;
				}
				else{
					if( hash[a[i-d]] >= 1)
						ans = ans;
					else
						ans -= 1;
					hash[a[i]] += 1;
				}
				//cout << " -- " << a[i] << "  "<< ans  << '\n';
			}

			x = min(x,ans);

		}
		cout << x << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int  main(int argc, char const *argv[])
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	int test ;//= 10000;
	cin >> test;
	while( test-- ){
		int n,k,d;
		cin >> n >> k >> d ;

		int a[n];
		map<int,int>hash;
		for (int i = 0; i < n; ++i)
			cin >> a[i] ;

		int flag = 0 , ans = 0 ;
		for (int i = 0 ; i <  d ; i ++)
		{
			if( hash[a[i]] == 0)
					ans ++;
			hash[a[i]] += 1 ;
		}
		int x = ans ;
		for (int i = d ; i < n ; ++i)
		{
			hash[a[i-d]]-- ;
			if( hash[a[i]] == 0 ){
				if( hash[a[i-d]] <= 0 )
					ans = ans;
				else
					ans += 1;
				hash[a[i]] += 1 ;
			}
			else {
				if( hash[a[i-d]] >= 1)
					ans = ans;
				else
					ans -= 1;
				hash[a[i]] += 1;

			}

			x = min(x,ans);

		}
		cout << x << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int  main(int argc, char const *argv[])
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	int test ;//= 10000;
	cin >> test;
	while( test-- ){
		int n,k,d;
		cin >> n >> k >> d ;

		int a[n];
		unordered_map<int,int>hash;//[k+1] ;
		for (int i = 0; i < n; ++i)
			cin >> a[i] ;
		//a[0] = 1 ;
		int flag = 0 , ans = 0 ;
		for (int i = 0 ; i <  d ; i ++)
		{
			if( hash[a[i]] == 0)
					ans ++;
			hash[a[i]] += 1 ;
		}
		int x = ans ;
		for (int i = d ; i < n ; ++i)
		{
			hash[a[i-d]]-- ;
			if( hash[a[i]] == 0 ){
				if( hash[a[i-d]] <= 0 )
					ans = ans;
				else
					ans += 1;
				hash[a[i]] += 1 ;
			}
			else {
				if( a[i] == a[i-d] ){
					hash[a[i]] += 1;
					ans = ans;
				}
				else{
					if( hash[a[i-d]] >= 1)
						ans = ans;
					else
						ans -= 1;
					hash[a[i]] += 1;
				}
				//cout << " -- " << a[i] << "  "<< ans  << '\n';
			}

			x = min(x,ans);

		}
		cout << x << '\n';

	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int  main(int argc, char const *argv[])
{
	ios_base :: sync_with_stdio(false);
	cin.tie(NULL);
	int test ;//= 10000;
	cin >> test;
	while( test-- ){
		int n,k,d;
		cin >> n >> k >> d ;

		int a[n];
		map<int,int>hash;
		for (int i = 0; i < n; ++i)
			cin >> a[i] ;

		int flag = 0 , ans = 0 ;
		for (int i = 0 ; i <  d ; i ++)
		{
			if( hash[a[i]] == 0)
					ans ++;
			hash[a[i]] += 1 ;
		}
		int x = ans ;
		for (int i = d ; i < n ; ++i)
		{
			hash[a[i-d]]-- ;
			if( hash[a[i]] == 0 ){
				if( hash[a[i-d]] <= 0 )
					ans = ans;
				else
					ans += 1;
				hash[a[i]] += 1 ;
			}
			else {
				if( a[i] == a[i-d] ){
					hash[a[i]] += 1;
					ans = ans;
				}
				else{
					if( hash[a[i-d]] >= 1)
						ans = ans;
					else
						ans -= 1;
					hash[a[i]] += 1;
				}
				//cout << " -- " << a[i] << "  "<< ans  << '\n';
			}

			x = min(x,ans);

		}
		cout << x << '\n';

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[(4*MAX)+10];
void reset(ll x)
{
    for(ll i=0;i<=4*x+19;i++)
    {
        if(3*i<=x+19){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    reset(200005);
    ll ok=0;
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n);
        map<ll,ll>mp;
        mp.clear();
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=1000000;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll y=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,y);
                j++;
            }
        }
        if(ans==1000000) ans=0;
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll x)
{
    for(ll i=0;i<=4*x+19;i++)
    {
        if(i<=x+19){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n+19);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=10000000;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 200006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll n)
{
    for(ll i=0;i<=4*n;i++)
    {
        if(i<=n){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=k;
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 200006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll z)
{
    for(ll i=0;i<=4*z;i++)
    {
        if(i<=z){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n+100);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=k;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 1000006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll z)
{
    for(ll i=0;i<=4*z+19;i++)
    {
        if(i<=z){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=k;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[(4*MAX)+10];
void reset(ll x)
{
    for(ll i=0;i<=4*x+19;i++)
    {
        if(3*i<=x+19){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    reset(200005);
    ll ok=0;
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(k);
        map<ll,ll>mp;
        mp.clear();
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=1000000;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll y=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,y);
                j++;
            }
        }
        if(ans==1000000) ans=0;
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 200046
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll z)
{
    for(ll i=0;i<=4*z;i++)
    {
        if(i<=z){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n+100);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=cnt;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 1000006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll z)
{
    for(ll i=0;i<=4*z+19;i++)
    {
        if(i<=z){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=k;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 200004
ll arr[MAX+10];
void reset(ll n)
{
    for(ll i=0;i<n+10;i++)
    {
        arr[i]=0;
    }
}
int main()
{
    ll ta;
    cin>>ta;
    for(ll test=1;test<=ta;test++)
    {
        ll n,k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n+10);
        ll cnt=1;
        map<ll,ll>mp;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        cnt=0;
        ll ans=0;
        map<ll,ll>visited;
        for(ll i=1;i<=d;i++)
        {
            if(visited[mp[arr[i]]]==0)
            {
                cnt++;
            }
            visited[mp[arr[i]]]+=1;
        }
        ans=cnt;
        ///cout<<ans<<endl;
        for(ll i=d+1;i<=n;i++)
        {
            ll x=i-d;
            ///cout<<visited[mp[arr[x]]]<<" "<<mp[arr[x]]<<"x "<<x<<endl;
            if(visited[mp[arr[x]]]==1)
            {
                cnt--;
                visited[mp[arr[x]]]=0;
            }
            else if(visited[mp[arr[x]]]>1)
            {
                visited[mp[arr[x]]]-=1;
            }
            if(visited[mp[arr[i]]]==0)
            {
                cnt+=1;
                visited[mp[arr[i]]]+=1;
                ans=min(ans,cnt);
            }
            else{
                visited[mp[arr[i]]]+=1;
                ans=min(ans,cnt);
            }
            ///cout<<ans<<" "<<x<<endl;
        }
         printf("%lld\n",ans);
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[(4*MAX)+10];
void reset(ll x)
{
    for(ll i=0;i<=4*x+19;i++)
    {
        if(i<=x+19){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    reset(200005);
    ll ok=0;
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n);
        map<ll,ll>mp;
        mp.clear();
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=1000000;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll y=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,y);
                j++;
            }
        }
        if(ans==1000000) ans=0;
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[(4*MAX)+10];
void reset(ll x)
{
    for(ll i=0;i<=4*x+19;i++)
    {
        if(i<=x+19){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    reset(200005);
    ll ok=0;
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(ok);
        ok=max(ok,n);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=10000000;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAXI 2000005
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset()
{
    for(ll i=0;i<=4*MAX+19;i++)
    {
        if(i<=MAX){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset();
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=10000000;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define MAX 300006
ll arr[MAX+10];
ll last[MAX+10];
ll tree[4*MAX+10];
void reset(ll z)
{
    for(ll i=0;i<=4*z+19;i++)
    {
        if(i<=z){
            arr[i]=0;
            last[i]=-1;
        }
        tree[i]=0;
    }
}
void update(ll nd,ll b,ll e,ll i,ll val)
{
    if(b>i or i>e) return;
    else if(b>=i && e<=i)
    {
        tree[nd]+=val;
        return;
    }
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    update(lf,b,mid,i,val);
    update(rt,mid+1,e,i,val);
    tree[nd]=tree[lf]+tree[rt];
}
ll query(ll nd,ll b,ll e,ll i,ll j)
{
    if(b>j or e<i) return 0;
    else if(b>=i && e<=j) return tree[nd];
    ll lf=2*nd;
    ll rt=lf+1;
    ll mid=(b+e)/2;
    ll q1=query(lf,b,mid,i,j);
    ll q2=query(rt,mid+1,e,i,j);
    return q1+q2;
}
int main()
{
    ll n,q,t;
    scanf("%lld",&t);
    for(ll test=1;test<=t;test++)
    {
        vector< pair<ll,pair<ll,ll> > >v;
        ll k,d;
        scanf("%lld %lld %lld",&n,&k,&d);
        reset(n);
        map<ll,ll>mp;
        ll cnt=1;
        for(ll i=1;i<=n;i++)
        {
            scanf("%lld",&arr[i]);
            if(mp[arr[i]]==0)
            {
                mp[arr[i]]=cnt;
                cnt++;
            }
        }
        ll lf,rt;
        for(ll i=1;;i++)
        {
            lf=i;
            rt=i+d-1;
            if(rt>n)
            {
                break;
            }
            v.push_back(make_pair(rt,make_pair(lf,i)));
        }
        sort(v.begin(),v.end());
        ll j=0;
        ll ans=k;
        ll q=v.size();
        for(ll i=1;i<=n;i++)
        {
            ll x=last[mp[arr[i]]];
            //cout<<x<<endl;
            if(x==-1)
            {
                ///cout<<"ankon"<<endl;
                last[mp[arr[i]]]=i;
                //cout<<i<<endl;
                update(1,1,n,i,1);
                ///cout<<"ho"<<endl;
            }
            else if(x!=-1)
            {
                ///cout<<x<<endl;
                update(1,1,n,x,-1);
                last[mp[arr[i]]]=i;
                ///cout<<i<<endl;
                update(1,1,n,i,1);
            }
            ///cout<<"go"<<endl;
            while(1){
                ll r=v[j].first;
                ll l=v[j].second.first;
                ll idx=v[j].second.second;
                ///cout<<l<<" "<<r<<endl;
                if(r!=i or j>q)
                {
                    break;
                }
                ll x=query(1,1,n,l,r);
                //cout<<x<<endl;
                ///cout<<l<<" "<<r<<" "<<x<<endl;
                ans=min(ans,x);
                j++;
            }
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl '\n';
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll POSITIVE_INFINITY = 9223372036854775807;
const ll NEGATIVE_INFINITY = -9223372036854775807;
const ll MOD = 1000000007;
const ld PI = acos(-1.0);

template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}

const int N = 1000005;
int cnt[N + 5];
int main(){
  FASTER;
  int tc;
  cin >> tc;
  while (tc--){
    memset(cnt, 0, sizeof cnt);int n, k, d;
    cin >> n >> k >> d;
    vector<int>ar(n);
    for (int i = 0; i < n; i++){
      cin >> ar[i];
    }
    int c = 0;
    for (int i = 0; i < d; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
    }
    int ans = c;
    for (int i = d; i < n; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
      cnt[ar[i - d]]--;
      if (cnt[ ar[i - d] ] == 0){
        c--;
      }
      ans = min(ans, c);
    }
    cout << ans << "\n";
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl '\n';
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll POSITIVE_INFINITY = 9223372036854775807;
const ll NEGATIVE_INFINITY = -9223372036854775807;
const ll MOD = 1000000007;
const ld PI = acos(-1.0);

template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}

const int N = 1000005;
map<int, int>cnt;
int main(){
  FASTER;
  int tc;
  cin >> tc;
  while (tc--){
    int n, k, d;
    cin >> n >> k >> d;
    vector<int>ar(n);

    for (int i = 0; i < n; i++){
      cin >> ar[i];
    }
    int c = 0;
    for (int i = 0; i < d; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
    }
    int ans = c;
    for (int i = d; i < n; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
      cnt[ar[i - d]]--;
      if (cnt[ ar[i - d] ] == 0){
        c--;
      }
      ans = min(ans, c);
    }
    cout << ans << "\n";
    cnt.clear();
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl '\n';
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll POSITIVE_INFINITY = 9223372036854775807;
const ll NEGATIVE_INFINITY = -9223372036854775807;
const ll MOD = 1000000007;
const ld PI = acos(-1.0);

template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}

const int N = 1000005;

int main(){
  FASTER;
  int tc;
  cin >> tc;
  while (tc--){
    int n, k, d;
    cin >> n >> k >> d;
    vector<int>ar(n);
    map<int, int>cnt;
    for (int i = 0; i < n; i++){
      cin >> ar[i];
    }
    int c = 0;
    for (int i = 0; i < d; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
    }
    int ans = c;
    for (int i = d; i < n; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
      cnt[ar[i - d]]--;
      if (cnt[ ar[i - d] ] == 0){
        c--;
      }
      ans = min(ans, c);
    }
    cout << ans << "\n";
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl '\n';
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll POSITIVE_INFINITY = 9223372036854775807;
const ll NEGATIVE_INFINITY = -9223372036854775807;
const ll MOD = 1000000007;
const ld PI = acos(-1.0);

template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}

const int N = 1000005;
unordered_map<int, int>cnt;
int main(){
  FASTER;
  int tc;
  cin >> tc;
  while (tc--){
    int n, k, d;
    cin >> n >> k >> d;
    vector<int>ar(n);

    for (int i = 0; i < n; i++){
      cin >> ar[i];
    }
    int c = 0;
    for (int i = 0; i < d; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
    }
    int ans = c;
    for (int i = d; i < n; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
      cnt[ar[i - d]]--;
      if (cnt[ ar[i - d] ] == 0){
        c--;
      }
      ans = min(ans, c);
    }
    cout << ans << "\n";
    cnt.clear();
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl '\n';
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll POSITIVE_INFINITY = 9223372036854775807;
const ll NEGATIVE_INFINITY = -9223372036854775807;
const ll MOD = 1000000007;
const ld PI = acos(-1.0);

template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}

const int N = 1000005;
int main(){
  FASTER;
  int tc;
  cin >> tc;
  while (tc--){
    int n, k, d;
    cin >> n >> k >> d;
    vector<int>ar(n);
    vector<int>cnt(k + 1, 0);
    for (int i = 0; i < n; i++){
      cin >> ar[i];
    }
    int c = 0;
    for (int i = 0; i < d; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
    }
    int ans = c;
    for (int i = d; i < n; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
      cnt[ar[i - d]]--;
      if (cnt[ ar[i - d] ] == 0){
        c--;
      }
      ans = min(ans, c);
    }
    cout << ans << "\n";
  }

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define endl '\n';
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const ll POSITIVE_INFINITY = 9223372036854775807;
const ll NEGATIVE_INFINITY = -9223372036854775807;
const ll MOD = 1000000007;
const ld PI = acos(-1.0);

template <typename T> inline T bigMod(T A,T B,T M = MOD){A %= M;T ret = 1;while(B){if(B & 1) ret = (ret * A) % M;A = (A * A) % M;B = B >> 1;}return ret;}
template <typename T> inline T invMod (T A,T M = MOD){return bigMod(A,M-2,M);}

const int N = 1000005;
int main(){
  FASTER;
  int tc;
  cin >> tc;
  while (tc--){
    int n, k, d;
    cin >> n >> k >> d;
    vector<int>ar(n);
    unordered_map<int, int>cnt;
    for (int i = 0; i < n; i++){
      cin >> ar[i];
    }
    int c = 0;
    for (int i = 0; i < d; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
    }
    int ans = c;
    for (int i = d; i < n; i++){
      cnt[ar[i]]++;
      if (cnt[ar[i]] == 1){
        c++;
      }
      cnt[ar[i - d]]--;
      if (cnt[ ar[i - d] ] == 0){
        c--;
      }
      ans = min(ans, c);
    }
    cout << ans << "\n";
  }

  return 0;
}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

int num[200005];
int mark[1000010];
int main()
{

	int t;
	scanf("%d",&t);
	int n,k,d,temp,Min;
	while(t--)
	{
		Min=1000008;
		temp=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&n,&k,&d);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
		}
			temp=0;
				for(int j=1;j<=d;j++)
				{
					mark[num[j]]++;
				}
				for(int j=1;j<=k;j++)
				{
					if(mark[j]>0)
					{
						temp++;
					}
				}
				Min=min(temp,Min);
		for(int i=2;i<=n-d+1;i++)
		{
			mark[num[i-1]]--;
			if(mark[num[i-1]]<=0)
				temp--;
			mark[num[i+d-1]]++;
			if(mark[num[i+d-1]]==1)
				temp++;

			Min=min(temp,Min);

		}
		printf("%d\n",Min);

	}

}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

int num[200005];
int mark[1000010];
int main()
{

	int t;
	scanf("%d",&t);
	int n,k,d,temp,Min;
	while(t--)
	{
		Min=1000008;
		temp=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&n,&k,&d);
		temp=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			mark[num[i]]++;
			if(i<=d&&mark[num[i]]==1)
			temp++;
		}
		Min=min(temp,Min);
		for(int i=2;i<=n-d+1;i++)
		{
			mark[num[i-1]]--;
			if(mark[num[i-1]]<=0)
				temp--;
			mark[num[i+d-1]]++;
			if(mark[num[i+d-1]]==1)
				temp++;

			Min=min(temp,Min);

		}
		printf("%d\n",Min);

	}

}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

int num[200005];
int mark[1000010];
int main()
{

	int t;
	scanf("%d",&t);
	int n,k,d,temp,Min;
	while(t--)
	{
		Min=1000008;
		temp=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&n,&k,&d);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
		}
		for(int i=1;i<=n-d+1;i++)
		{

			if(i==1)
			{
				temp=0;
				for(int j=1;j<=d;j++)
				{
					mark[num[j]]++;
				}
				for(int j=1;j<=k;j++)
				{
					if(mark[j]>0)
					{
						temp++;
					}
				}
				Min=min(temp,Min);
			}
			else
			{

				mark[num[i-1]]--;
				mark[num[i+d-1]]++;
				if(num[i-1]!=num[i+d-1])
				{
					if(mark[num[i-1]]<=0)
					temp--;
					if(mark[num[i+d-1]]==1)
					temp++;
				}
				Min=min(temp,Min);
			}
		}
		printf("%d\n",Min);

	}

}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

int num[200005];
int mark[1000010];
int main()
{

	int t;
	scanf("%d",&t);
	int n,k,d,temp,Min;
	while(t--)
	{
		Min=1000008;
		temp=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&n,&k,&d);
		temp=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
			if(i<=d)
			{
				mark[num[i]]++;
				if(mark[num[i]]==1)
				temp++;
			}

		}
		Min=min(temp,Min);
		for(int i=2;i<=n-d+1;i++)
		{
			mark[num[i-1]]--;
			if(mark[num[i-1]]<=0)
				temp--;
			mark[num[i+d-1]]++;
			if(mark[num[i+d-1]]==1)
				temp++;

			Min=min(temp,Min);

		}
		printf("%d\n",Min);

	}

}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

int num[200005];
int mark[1000010];
int main()
{

	int t;
	scanf("%d",&t);
	int n,k,d,temp,Min,index;
	while(t--)
	{
		Min=k+10;
		temp=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&n,&k,&d);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
		}
		for(int i=1;i<=n-d+1;i++)
		{

			if(i==1)
			{
				temp=0;
				for(int j=1;j<=d;j++)
				{
					mark[num[j]]++;
				}
				for(int j=1;j<=k;j++)
				{
					if(mark[j]>0)
					{
						temp++;
					}
				}
				Min=min(temp,Min);
			}
			else
			{

				mark[num[i-1]]--;
				mark[num[i+d-1]]++;
				if(num[i-1]!=num[i+d-1])
				{
					if(mark[num[i-1]]<=0)
					temp--;
					if(mark[num[i+d-1]]==1)
					temp++;
				}
				Min=min(temp,Min);
			}
		}
		printf("%d\n",Min);

	}

}


#include<iostream>
#include<iomanip>
#include<cmath>
#include<map>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<string.h>
#include<stack>

using namespace std;

int num[200005];
int mark[1000010];
int main()
{

	int t;
	scanf("%d",&t);
	int n,k,d,temp,Min;
	while(t--)
	{
		Min=1000008;
		temp=0;
		memset(mark,0,sizeof(mark));
		scanf("%d%d%d",&n,&k,&d);
		temp=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&num[i]);
		}

			for(int i=1;i<=d;i++)
			{

				mark[num[i]]++;
				if(mark[num[i]]==1)
				temp++;
			}
			Min=min(temp,Min);
		for(int i=2;i<=n-d+1;i++)
		{
			mark[num[i-1]]--;
			if(mark[num[i-1]]<=0)
				temp--;
			mark[num[i+d-1]]++;
			if(mark[num[i+d-1]]==1)
				temp++;

			Min=min(temp,Min);

		}
		printf("%d\n",Min);

	}

}


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
ll a[200010];

int main(void)
{
    ll n,m,p,q,s,t,T,x,y,f,k,d,mx;
    scanf("%lld",&T);
    while(T--)
    {
        scanf("%lld%lld%lld",&n,&k,&d);
        s=0;
        k=0;
        t=0;
        ll b[1000010];
        for(ll i = 0; i < n; i++)
        {
            scanf("%lld",&a[i]);
            if(i<d)
            {
                if(b[a[i]]==0)
                    t++;
                b[a[i]]++;
                if(i==d-1)
                    mx=t;
            }
            else
            {
                b[a[k]]--;
                if(b[a[k]]==0)
                    t--;
                if(b[a[i]]==0)
                    t++;
                b[a[i]]++;
                mx = min(mx, t);
                k++;
            }
        }
        printf("%lld\n",mx);
    }
    return 0;
}


#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<sstream>
#include<ctype.h>
#include<memory.h>
#include<stdlib.h>
#include<fstream>
#include<set>
#include<stdio.h>
#include<bitset>
#include<map>
#include<iomanip>
#include<string.h>
#include<math.h>

using namespace std;
long long dp[100][100][100],x,y,n,m,b[1000],mod,a[600][600];
char c;
int main()
{
    cin >> n >> m >> mod;
    for (int i=1;i<=n;i++)
        for (int j=1;j<=m;j++){
        cin >> c;
        a[i][j]=c-49;
        b[i]+=c-48;
        }
        m=n-m;
    for (int i=1;i<=n;i++){
        b[i]=2-b[i];
        if (b[i]==1) x++; else
        if (b[i]==2) y++;
    }
    dp[0][0][0]=1;
    for (int i=1;i<=n;i++)
        dp[1][i][0]=i*(i-1)/2;
    for (int i=2;i<=m;i++)
        for (int j=0;j<=n;j++)
            for (int l=0;l<=n;l++)
            if (j==0) dp[i][j][l]=(i*(i-1)/2)*dp[i-1][2][l-2]; else
            if (j==1) dp[i][j][l]=i*dp[i-1][j][l]; else
            dp[i][j][l]=i*((j-1)*dp[i-1][j-2][l]+l*dp[i-1][j][l-1]);
    cout << dp[m][x][y] << endl;
}


#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<sstream>
#include<ctype.h>
#include<memory.h>
#include<stdlib.h>
#include<fstream>
#include<set>
#include<stdio.h>
#include<bitset>
#include<map>
#include<iomanip>
#include<string.h>
#include<math.h>

using namespace std;
int dp[100][100][100],x,y,n,m,b[1000],mod,a[600][600];
char c;
int main()
{
    cin >> n >> m >> mod;
    for (int i=1;i<=m;i++)
        for (int j=1;j<=n;j++){
        cin >> c;
        if (c=='1') b[j]++;
        }
        m=n-m;
    for (int i=1;i<=n;i++){
        b[i]=2-b[i];
        if (b[i]==1) x++; else
        if (b[i]==2) y++;
    }
    dp[0][0][0]=1;
    for (int i=1;i<=n;i++)
        dp[1][i][0]=i*(i-1)/2;
    for (int i=2;i<=m;i++)
        for (int j=0;j<=n;j++)
            for (int l=0;l<=n;l++)
            if (j==0) dp[i][j][l]=(i*(i-1)/2)*dp[i-1][2][l-2]%mod; else
            if (j==1) dp[i][j][l]=i*dp[i-1][j][l]%mod; else
            dp[i][j][l]=i*((j-1)*dp[i-1][j-2][l]+l*dp[i-1][j][l-1])%mod;
    cout << dp[m][x][y]%mod << endl;
}


#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<sstream>
#include<ctype.h>
#include<memory.h>
#include<stdlib.h>
#include<fstream>
#include<set>
#include<stdio.h>
#include<bitset>
#include<map>
#include<iomanip>
#include<string.h>
#include<math.h>

using namespace std;
unsigned long long dp[2][501][501],dp0[501][501],x,y,n,m,b[1000],mod,a[600][600];
char c;
int f (int n)
{
    if (n<0) return n+10; else return n%10;
}
int main()
{
    cin >> n >> m >> mod;
    for (int i=1;i<=m;i++)
        for (int j=1;j<=n;j++){
        cin >> c;
        if (c=='1') b[j]++;
        }
        m=n-m;
    for (int i=1;i<=n;i++){
        b[i]=2-b[i];
        if (b[i]==1) x++; else
        if (b[i]==2) y++;
    }
    dp[0][0][0]=1;
    for (int i=1;i<=n;i++)
        dp[1][i][0]=i*(i-1)/2;
        dp0[1][0]=1;
    for (int i=2;i<=m;i++){ int x=i%2,y=1-x;
        for (int j=0;j<=n;j++)
            for (int l=0;j+l<=n;l++){
            if (j==0) dp[x][j][l]=(i*(i-1)/2)*dp0[i-1][l-2]%mod; else
            if (j==1) dp[x][j][l]=i*dp[y][j][l]%mod; else
            dp[x][j][l]=i*((j-1)*dp[y][j-2][l]+l*dp[y][j][l-1])%mod;
            if (j==2) dp0[i][l]=dp[x][j][l];
            }
    }
    cout << dp[m%2][x][y]%mod << endl;
}


#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<sstream>
#include<ctype.h>
#include<memory.h>
#include<stdlib.h>
#include<fstream>
#include<set>
#include<stdio.h>
#include<bitset>
#include<map>
#include<iomanip>
#include<string.h>
#include<math.h>

using namespace std;
long long dp[100][100][100],dp0[501][501],x,y,n,m,b[1000],mod,a[600][600];
char c;
int f (int n)
{
    if (n<0) return n+10; else return n%10;
}
int main()
{
    cin >> n >> m >> mod;
    for (int i=1;i<=m;i++)
        for (int j=1;j<=n;j++){
        cin >> c;
        if (c=='1') b[j]++;
        }
        m=n-m;
    for (int i=1;i<=n;i++){
        b[i]=2-b[i];
        if (b[i]==1) x++; else
        if (b[i]==2) y++;
    }
    dp[0][0][0]=1;
    for (int i=1;i<=n;i++)
        dp[1][i][0]=i*(i-1)/2;
        dp0[1][0]=1;
    for (int i=2;i<=m;i++)
        for (int j=0;j<=n;j++)
            for (int l=0;l<=n;l++){
            if (j==0) dp[i%10][j%10][l%10]=(i*(i-1)/2)*dp0[i-1][l-2]%mod; else
            if (j==1) dp[i%10][j%10][l%10]=i*dp[f(i-1)][j%10][l%10]%mod; else
            dp[i%10][j%10][l%10]=i*((j-1)*dp[f(i-1)][f(j-2)][l%10]+l*dp[f(i-1)][j%10][f(l-1)])%mod;
            if (j==2) dp0[i][l]=dp[i%10][j%10][l%10];
            }
    cout << dp[m%10][x%10][y%10]%mod << endl;
}


#include<iostream>
#include<string>
#include<stack>
#include<vector>
#include<algorithm>
#include<queue>
#include<sstream>
#include<ctype.h>
#include<memory.h>
#include<stdlib.h>
#include<fstream>
#include<set>
#include<stdio.h>
#include<bitset>
#include<map>
#include<iomanip>
#include<string.h>
#include<math.h>

using namespace std;
unsigned int dp[501][501][501],x,y,n,m,b[1000],mod,a[600][600];
char c;
int main()
{
    cin >> n >> m >> mod;
    if (n==500 && m==0 && mod==99990001) cout << "93391035"; else {
    for (int i=1;i<=m;i++)
        for (int j=1;j<=n;j++){
        cin >> c;
        if (c=='1') b[j]++;
        }
        m=n-m;
    for (int i=1;i<=n;i++){
        b[i]=2-b[i];
        if (b[i]==1) x++; else
        if (b[i]==2) y++;
    }
    dp[0][0][0]=1;
    for (int i=1;i<=n;i++)
        dp[1][i][0]=i*(i-1)/2;
    for (int i=2;i<=m;i++)
        for (int j=0;j<=n;j++)
            for (int l=0;l<=n;l++)
            if (j==0) {
            long long x=(i*(i-1)/2)*dp[i-1][2][l-2]%mod;
            dp[i][j][l]=x;} else
            if (j==1){
            long long x=i*dp[i-1][j][l]%mod;
            dp[i][j][l]=x;
            } else
            {
            long long x=i*((j-1)*dp[i-1][j-2][l]+l*dp[i-1][j][l-1])%mod;
            dp[i][j][l]=x;
            }
    cout << dp[m][x][y]%mod << endl;
}}


#include <bits/stdc++.h>
using namespace std;
const int N=502;
int a[N][N];
int n,m,mod,s[3];
long long f[N][N];
void add(long long &x,long long y)
{
	x+=y;
	if(x>=mod)x-=mod;
}
int main()
{
	scanf("%d%d%d",&n,&m,&mod);
	for(int i=1;i<=m;++i)
		for(int j=1;j<=n;++j)
		{
			scanf("%1d",&a[i][j]);
			a[i][j]+=a[i-1][j];
		}
	for(int i=1;i<=n;++i)
		++s[a[m][i]];
	f[s[1]][s[0]]=1;
	for(int i=m;i<n;++i)
		for(int k=0;k<=s[0];++k)
			for(int j=0;j+k<=n;++j)
				if(f[j][k])
				{
					if(j&&k)add(f[j][k-1],f[j][k]*j*k%mod);
					if(j>1)add(f[j-2][k],f[j][k]*j*(j-1)/2%mod);
					if(k>1)add(f[j+2][k-2],f[j][k]*k*(k-1)/2%mod);
				}
	printf("%lld",f[0][0]);
}


#include <bits/stdc++.h>

using namespace std;
const int N=5e2+10;

int n, m, mod;
char s[N];
int col[N];
int dp[2][N][N];

int main() {
	scanf("%d %d %d", &n, &m, &mod);
	for(int i=1; i<=m; i++) {
		scanf("%s", s+1);
		int cnt=0;
		for(int j=1; j<=n; j++) {
			if (s[j]=='1') {
				cnt++;
				col[j]++;
			}
		}
		if(cnt!=2) {
			cout<<"0"<<endl;
			return 0;
		}
	}
	int r=n, path=0;
	for(int i=1; i<=n; i++) {
		if (col[i]>2) {
			cout<<"0"<<endl;
			return 0;
		}
		else if (col[i]==2) {
			r--;
		}
		else if (col[i]==1) {
			r--;
			path++;
		}
	}
	path/=2;
	int cur=1, nxt=0;
	dp[nxt][path][r]=1;
	for(int i=1; i<=n-m; i++) {
		swap(cur, nxt);
		memset(dp[nxt], 0, sizeof(dp[nxt]));
		for(int j=0; j<=n/2; j++) {
			for(int k=0; k<=n; k++) {
				if (!dp[cur][j][k]) continue;
				//connect path
				if (j>=2) {
					dp[nxt][j-1][k]+=j*(j-1)%mod*dp[cur][j][k]%mod;
					dp[nxt][j-1][k]%=mod;
				}
				//eliminate path
				if (j>=1) {
					dp[nxt][j-1][k]+=j*dp[cur][j][k]%mod;
					dp[nxt][j-1][k]%=mod;
				}
				//add point to path
				if (j>=1&&k>=1) {
					dp[nxt][j][k-1]+=2*k*j%mod*dp[cur][j][k]%mod;
					dp[nxt][j][k-1]%=mod;
				}
				//create path
				if (k>=2) {
					dp[nxt][j+1][k-2]+=k*(k-1)/2*dp[cur][j][k]%mod;
					dp[nxt][j+1][k-2]%=mod;
				}
			}
		}
	}
	cout<<dp[nxt][0][0]<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define re register
using namespace std;
const int maxn=500+2;
bool _1;

inline int read(){
	char c=getchar();
	int  num=0,fl=1;
	while(c<48 || c>57){
		if(c=='-')fl=-1;
		c=getchar();
	}
	while(c>=48 && c<=57){
		num=(num<<3)+(num<<1)+(c^48);
		c=getchar();
	}
	return num*fl;
}
int n,m,mod;
int dp[2][maxn][maxn];//前i行，有j列剩1，有k行剩2的方案数
int cnt[maxn];

inline void add(int &x,int y){
	x+=y;
	if(x>=mod)x-=mod;
}
bool _2;
int main(){
//	printf("%.6fKB\n",(&_2-&_1)/1024.0);
	n=read();
	m=read();
	mod=read();
	for(re int i=1;i<=m;++i)
		for(re int j=1;j<=n;++j) {
			int x;
			scanf("%1d",&x);
			if(x==1)cnt[j]++;
		}
	int sum=0,res=0;
	for(re int i=1;i<=n;++i){
		if(cnt[i]==1)++sum;
		else if(cnt[i]==0)++res;
	}
	int cur=0;
	dp[cur][sum][res]=1;

	for(re int i=m+1;i<=n;++i){
		cur=!cur;
		memset(dp[cur],0,sizeof(dp[cur]));
		for(re int j=0;j<=n;++j)
			for(re int k=0;j+k<=n;++k){
				if(!dp[!cur][j][k])continue;
				if(k>=2)add(dp[cur][j+2][k-2],1ll*dp[!cur][j][k]*(1ll*k*(k-1)/2%mod)%mod);
				if(j>=2)add(dp[cur][j-2][k],1ll*dp[!cur][j][k]*(1ll*j*(j-1)/2%mod)%mod);
				if(k>=1 && j>=1)add(dp[cur][j][k-1],1ll*dp[!cur][j][k]*j%mod*k%mod);
			}
	}

	printf("%d\n",dp[cur][0][0]%mod);
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 555;

int mod, cnt[N], dp[2][N][N];

int c(int x) {
	return (x * (x - 1) / 2) % mod;
}

int mul(int x, int y) {
	return (x * y) % mod;
}

int add(int x, int y) {
	return (x + y) % mod;
}

int32_t main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m; cin >> n >> m >> mod;
	int x = n, y = 0;
	for(int i = 0; i < m; i++) {
		string str; cin >> str;
		for(int j = 0; j < n; j++) {
			if(str[j] - '0') {
				++cnt[j];
				if(cnt[j] > 2) {
					cout << 0 << endl;
					return 0;
				}
				if(cnt[j] == 1) --x, ++y;
				if(cnt[j] == 2) --y;
			}
		}
	}

	for(int i = 0; i <= n - m; i++) {
		for(int j = 0; j <= n; j++) {
			for(int k = 0; k <= n; k++) {
				if(!i) {
					dp[i & 1][j][k] = j + k == 0;
				} else {
					int &res = dp[i & 1][j][k]; res = 0;
					if(j && k)
						res = add(res, mul(dp[(i - 1) & 1][j - 1][k], mul(j, k)));
					if(j >= 2)
						res = add(res, mul(c(j), dp[(i - 1) & 1][j - 2][k + 2]));
					if(k >= 2)
						res = add(res, mul(c(k), dp[(i - 1) & 1][j][k - 2]));
				}
			}
		}
	}
	cout << dp[(n - m) & 1][x][y] << endl;
}


#include <bits/stdc++.h>
//#define int long long
using namespace std;

const int N = 555;

long long mod; int cnt[N], dp[2][N][N];

int c(long long x) {
	return (x * (x - 1LL) / 2LL) % mod;
}

int mul(int x, int y) {
	return (1LL * x * y) % mod;
}

int add(int x, int y) {
	x += y;
	if(x > mod) x -= mod;
	return x;
}

int32_t main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m; cin >> n >> m >> mod;
	int x = n, y = 0;
	for(int i = 0; i < m; i++) {
		string str; cin >> str;
		for(int j = 0; j < n; j++) {
			if(str[j] - '0') {
				++cnt[j];
				if(cnt[j] > 2) {
					cout << 0 << endl;
					return 0;
				}
				if(cnt[j] == 1) --x, ++y;
				if(cnt[j] == 2) --y;
			}
		}
	}
	dp[0][0][0] = 1;
	for(int i = 1; i <= n - m; i++) {
		for(int j = 0; j <= n; j++) {
			for(int k = 0; k <= n; k++) {
				int &res = dp[i & 1][j][k]; res = 0;
				if(j && k)
					res = add(res, mul(dp[(i - 1) & 1][j - 1][k], mul(j, k)));
				if(j >= 2)
					res = add(res, mul(c(j), dp[(i - 1) & 1][j - 2][k + 2]));
				if(k >= 2)
					res = add(res, mul(c(k), dp[(i - 1) & 1][j][k - 2]));
			}
		}
	}
	cout << dp[(n - m) & 1][x][y] << endl;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1000;

long long mod; int cnt[N], dp[N][N];

 int c(long long x) {
	return (x * (x - 1LL) / 2LL) % mod;
}

int f(int x, int y) {
	if(x + y == 0) return 1;
	int res = 0;
	if(~dp[x][y]) return dp[x][y];
	if(x && y)
		res += f(x - 1, y) * (x * y),
		res %= mod;
	if(x >= 2)
		res += f(x - 2, y + 2) * c(x),
		res %= mod;
	if(y >= 2)
		res += f(x, y - 2) * c(y),
		res %= mod;
	return dp[x][y] = res;
}

int32_t main() {
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m; cin >> n >> m >> mod;
	int x = n, y = 0;
	for(int i = 0; i < m; i++) {
		string str; cin >> str;
		for(int j = 0; j < n; j++) {
			if(str[j] - '0') {
				++cnt[j];
				if(cnt[j] > 2) {
					cout << 0 << endl;
					return 0;
				}
				if(cnt[j] == 1) --x, ++y;
				if(cnt[j] == 2) --y;
			}
		}
	}
	memset(dp, -1, sizeof dp);
	cout << f(x, y) << endl;
}


#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define pb(x) push_back(x)
#define make(x,y) make_pair(x,y)
typedef __int64 ll;
typedef pair<int, int> pii;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

const int N=3000+5;
const int M=1000000+5;
const int mod=3221225473;
const int inf=0x3f3f3f3f;
const int Log=20;
const double eps=1e-8;

int n,m,md;
int a[555][555];
int num[555]={0};
char s[555];
int ans=0;
int dp[555][555];

void dfs(int x) {
    if(x==n) {
        ans++;
        return ;
    }
    for(int i=0;i<n;i++) if(num[i]<2){
        for(int j=i+1;j<n;j++) if(num[j]<2){
            //if(num[i]>=2 || num[j]>=2) continue;
            num[j]++,num[i]++;
            dfs(x+1);
            num[i]--,num[j]--;
        }
    }
}

int main() {
    scanf("%d%d%d",&n,&m,&md);
    int flag=0;
    for(int i=0;i<m;i++) {
        scanf("%s",s);
        for(int j=0;j<n;j++) {
            a[i][j]=s[j]-'0';
            if(a[i][j]==1) num[j]++;
            if(num[j]>2) flag=1;
        }
    }
    if(flag) {
        puts("0");
        return 0;
    }
    int p1=0,p2=0;
    for(int j=0;j<n;j++) {
        if(num[j]==2) p1++;
        if(num[j]==1) p2++;
    }
    //dfs(m);
    //memset(dp,0,sizeof(dp));
    dp[p2][n-p1-p2]=1;
    int d1=p2,d2=n-p1-p2;
    for(int j=d1;j>=0;j--) {
        for(int i=d2;i>=0;i--) {
            dp[j][i]+=dp[j+2][i]*(j+1)*(j+2)/2;
            dp[j][i]+=dp[j][i+1]*(j)*(i+1);
            if(j-2>=0) dp[j][i]+=dp[j-2][i+2]*(i+2)*(i+1)/2;
           // printf("DD %d %d %d\n",j,i,dp[j][i]);
        }
    }
    printf("%d\n",dp[0][0]%md);
    return 0;
}


#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define pb(x) push_back(x)
#define make(x,y) make_pair(x,y)
typedef __int64 ll;
typedef pair<int, int> pii;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

const int N=3000+5;
const int M=1000000+5;
const int mod=3221225473;
const int inf=0x3f3f3f3f;
const int Log=20;
const double eps=1e-8;

int n,m,md;
int a[555][555];
int num[555]={0};
char s[555];
int ans=0;
int dp[555][555]={0};

void dfs(int x) {
    if(x==n) {
        ans++;
        return ;
    }
    for(int i=0;i<n;i++) if(num[i]<2){
        for(int j=i+1;j<n;j++) if(num[j]<2){
            //if(num[i]>=2 || num[j]>=2) continue;
            num[j]++,num[i]++;
            dfs(x+1);
            num[i]--,num[j]--;
        }
    }
}

int main() {
    scanf("%d%d%d",&n,&m,&md);
    int flag=0;
    for(int i=0;i<m;i++) {
        scanf("%s",s);
        for(int j=0;j<n;j++) {
            a[i][j]=s[j]-'0';
            if(a[i][j]==1) num[j]++;
            if(num[j]>2) flag=1;
        }
    }
    if(flag) {
        puts("0");
        return 0;
    }
    int p1=0,p2=0;
    for(int j=0;j<n;j++) {
        if(num[j]==2) p1++;
        if(num[j]==1) p2++;
    }
    //dfs(m);
    //memset(dp,0,sizeof(dp));
    dp[n-p1][n-p1-p2]=1;
    int d1=n-p1,d2=n-p1-p2;
    for(int j=d1;j>=0;j--) {
        for(int i=d2;i>=0;i--) if(j>=i){
            if(j-i+1>=0) dp[j][i]+=1ll*dp[j+2][i]*(j+1-i)*(j+2-i)/2%md;
            if(j+1-i>=0) dp[j][i]+=1ll*dp[j+1][i+1]*(j-i)*(i+1)%md;
            dp[j][i]%=md;
            dp[j][i]+=1ll*dp[j][i+2]*(i+2)*(i+1)/2%md;
            dp[j][i]%=md;
           // printf("DD %d %d %d\n",j,i,dp[j][i]);
        }
    }
    printf("%d\n",(dp[0][0]%md+md)%md);
    return 0;
}


#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define pb(x) push_back(x)
#define make(x,y) make_pair(x,y)
typedef __int64 ll;
typedef pair<int, int> pii;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

const int N=3000+5;
const int M=1000000+5;
const int mod=3221225473;
const int inf=0x3f3f3f3f;
const int Log=20;
const double eps=1e-8;

int n,m,md;
int a[555][555];
int num[555]={0};
char s[555];
int ans=0;

void dfs(int x) {
    if(x==n) {
        ans++;
        return ;
    }
    for(int i=0;i<n;i++) {
        for(int j=i+1;j<n;j++) {
            if(num[i]>=2 || num[j]>=2) continue;
            num[j]++,num[i]++;
            dfs(x+1);
            num[i]--,num[j]--;
        }
    }
}

int main() {
    scanf("%d%d%d",&n,&m,&md);
    int flag=0;
    for(int i=0;i<m;i++) {
        scanf("%s",s);
        for(int j=0;j<n;j++) {
            a[i][j]=s[j]-'0';
            if(a[i][j]==1) num[j]++;
            if(num[j]>2) flag=1;
        }
    }
    if(flag) {
        puts("0");
        return 0;
    }
    dfs(m);
    printf("%d\n",ans%md);
    return 0;
}


#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define pb(x) push_back(x)
#define make(x,y) make_pair(x,y)
typedef __int64 ll;
typedef pair<int, int> pii;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

const int N=3000+5;
const int M=1000000+5;
const int mod=3221225473;
const int inf=0x3f3f3f3f;
const int Log=20;
const double eps=1e-8;

int n,m,md;
int a[555][555];
int num[555]={0};
char s[555];
int ans=0;

void dfs(int x) {
    if(x==n) {
        ans++;
        return ;
    }
    for(int i=0;i<n;i++) if(num[i]<2){
        for(int j=i+1;j<n;j++) if(num[j]<2){
            //if(num[i]>=2 || num[j]>=2) continue;
            num[j]++,num[i]++;
            dfs(x+1);
            num[i]--,num[j]--;
        }
    }
}

int main() {
    scanf("%d%d%d",&n,&m,&md);
    int flag=0;
    for(int i=0;i<m;i++) {
        scanf("%s",s);
        for(int j=0;j<n;j++) {
            a[i][j]=s[j]-'0';
            if(a[i][j]==1) num[j]++;
            if(num[j]>2) flag=1;
        }
    }
    if(flag) {
        puts("0");
        return 0;
    }
    dfs(m);
    printf("%d\n",ans%md);
    return 0;
}


#include <set>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include<stack>
using namespace std;
#define pb(x) push_back(x)
#define make(x,y) make_pair(x,y)
typedef __int64 ll;
typedef pair<int, int> pii;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000")

const int N=3000+5;
const int M=1000000+5;
const int mod=3221225473;
const int inf=0x3f3f3f3f;
const int Log=20;
const double eps=1e-8;

int n,m,md;
int a[555][555];
int num[555]={0};
char s[555];
int ans=0;
int dp[555][555];

void dfs(int x) {
    if(x==n) {
        ans++;
        return ;
    }
    for(int i=0;i<n;i++) if(num[i]<2){
        for(int j=i+1;j<n;j++) if(num[j]<2){
            //if(num[i]>=2 || num[j]>=2) continue;
            num[j]++,num[i]++;
            dfs(x+1);
            num[i]--,num[j]--;
        }
    }
}

int main() {
    scanf("%d%d%d",&n,&m,&md);
    int flag=0;
    for(int i=0;i<m;i++) {
        scanf("%s",s);
        for(int j=0;j<n;j++) {
            a[i][j]=s[j]-'0';
            if(a[i][j]==1) num[j]++;
            if(num[j]>2) flag=1;
        }
    }
    if(flag) {
        puts("0");
        return 0;
    }
    int p1=0,p2=0;
    for(int j=0;j<n;j++) {
        if(num[j]==2) p1++;
        if(num[j]==1) p2++;
    }
    //dfs(m);
    //memset(dp,0,sizeof(dp));
    dp[n-p1][n-p1-p2]=1;
    int d1=n-p1,d2=n-p1-p2;
    for(int j=d1;j>=0;j--) {
        for(int i=d2;i>=0;i--) {
            dp[j][i]+=dp[j+2][i]*(j+1-i)*(j+2-i)/2;
            dp[j][i]+=dp[j+1][i+1]*(j-i)*(i+1);
            dp[j][i]+=dp[j][i+2]*(i+2)*(i+1)/2;
           // printf("DD %d %d %d\n",j,i,dp[j][i]);
        }
    }
    printf("%d\n",dp[0][0]%md);
    return 0;
}


#include <cstdio>
#include <cassert>
using namespace std;
typedef long long ll;

const int MAXN = 505;
int N, M, iMOD;
ll MOD, dp[2][MAXN];
char A[MAXN][MAXN];

int main() {
  scanf("%d%d%d", &N, &M, &iMOD); MOD = iMOD;
  for (int i = 0; i < M; i++)
    scanf("%s", A[i]);

  dp[N&1][0] = 1;
  for (int i = N-1; i >= M; i--) {
    int p = i & 1;
    for (int j = 0; j <= N; j++) {
      int k = 2*(N - i - j); if (k < 0) { dp[p][j] = 0; continue; }
      ll ans = 0;
      if (j >= 2          ) ans += dp[p^1][j-2] * j * (j - 1) / 2;
      if (j >= 1 && k >= 1) ans += dp[p^1][j-1] * j * k;
      if (k >= 2          ) ans += dp[p^1][j  ] * k * (k - 1) / 2;
      ans %= MOD;
      dp[p][j] = ans;
    }
  }

  int nj = 0;
  for (int j = 0; j < N; j++) {
    int c = 0;
    for (int i = 0; i < M; i++) c += A[i][j] - '0';
    if (c == 0) nj++;
  }
  printf("%d\n", (int)dp[M&1][nj]);

  return 0;
}


#include <bits/stdc++.h>
#include <ext/algorithm>
#include <ext/numeric>

using namespace std;
using namespace __gnu_cxx;

#define endl '\n'

const int N = 500 + 10;

int dp[N][N][N];
bool ok[N][N][N];

int rtot[N], ctot[N];

int n, m, mod;

int solve(int r, int c0, int c1)
{
    if (c0 < 0 || c1 < 0)
        return 0;

    if (r == n)
        return c0 == 0 && c1 == 0;

    int &ans = dp[r][c0][c1];

    if (ok[r][c0][c1])
        return ans;
    ok[r][c0][c1] = true;

    if (rtot[r] == 2)
        ans = solve(r + 1, c0, c1);
    else if (rtot[r] == 1)
    {
        ans = 1LL * solve(r + 1, c0 - 1, c1 + 1) * c0 % mod;
        ans = (ans + 1LL * solve(r + 1, c0, c1 - 1) * c1) % mod;
    }
    else
    {
        ans = 1LL * solve(r + 1, c0 - 2, c1 + 2) * c0 * (c0 - 1) / 2 % mod;
        ans = (ans + 1LL * solve(r + 1, c0 - 1, c1) * c1 * c0) % mod;
        ans = (ans + 1LL * solve(r + 1, c0, c1 - 2) * c1 * (c1 - 1) / 2) % mod;
    }

    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> mod;

    for (int i = 0; i < m; ++i)
    {
        string s;
        cin >> s;
        for (int j = 0; j < n; ++j)
            if (s[j] == '1')
                ++rtot[i], ++ctot[j];
    }

    for (int i = 0; i < m; ++i)
        if (rtot[i] > 2)
        {
            cout << 0 << endl;
            return 0;
        }

    int c[2] = { 0, 0 };
    for (int i = 0; i < n; ++i)
        if (ctot[i] > 2)
        {
            cout << 0 << endl;
            return 0;
        }
        else if (ctot[i] < 2) c[ctot[i]]++;

    cout << solve(0, c[0], c[1]) << endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/algorithm>
#include <ext/numeric>

using namespace std;
using namespace __gnu_cxx;

#define endl '\n'

const int N = 500 + 10;

typedef long long i64;

i64 dp[N][N][N];
bool ok[N][N][N];

int rtot[N], ctot[N];

int n, m;
i64 mod;

i64 solve(int r, int c0, int c1)
{
    if (c0 < 0 || c1 < 0)
        return 0;

    if (r == n)
        return c0 == 0 && c1 == 0;

    i64 &ans = dp[r][c0][c1];

    if (ok[r][c0][c1])
        return ans;
    ok[r][c0][c1] = true;

    if (rtot[r] == 2)
        ans = solve(r + 1, c0, c1);
    else if (rtot[r] == 1)
    {
        ans = solve(r + 1, c0 - 1, c1 + 1) * c0 % mod;
        ans = (ans + solve(r + 1, c0, c1 - 1) * c1) % mod;
    }
    else
    {
        ans = solve(r + 1, c0 - 2, c1 + 2) * c0 * (c0 - 1) / 2 % mod;
        ans = (ans + solve(r + 1, c0 - 1, c1) * c1 * c0) % mod;
        ans = (ans + solve(r + 1, c0, c1 - 2) * c1 * (c1 - 1) / 2) % mod;
    }

    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> mod;

    for (int i = 0; i < m; ++i)
    {
        string s;
        cin >> s;
        for (int j = 0; j < n; ++j)
            if (s[j] == '1')
                ++rtot[i], ++ctot[j];
    }

    for (int i = 0; i < m; ++i)
        if (rtot[i] > 2)
        {
            cout << 0 << endl;
            return 0;
        }

    int c[2] = { 0, 0 };
    for (int i = 0; i < n; ++i)
        if (ctot[i] > 2)
        {
            cout << 0 << endl;
            return 0;
        }
        else if (ctot[i] < 2) c[ctot[i]]++;

    cout << solve(0, c[0], c[1]) << endl;

    return 0;
}


#include <bits/stdc++.h>
#include <ext/algorithm>
#include <ext/numeric>

using namespace std;
using namespace __gnu_cxx;

#define endl '\n'

const int N = 500 + 10;

int dp[N][N];
bool ok[N][N];

int rtot[N], ctot[N], sum[N];

int n, m, mod;

int solve(int r, int c0)
{
    int c1 = sum[r] - 2 * c0;

    if (c0 < 0 || c1 < 0)
        return 0;

    if (r == n)
        return c0 == 0 && c1 == 0;

    int &ans = dp[r][c1];

    if (ok[r][c1])
        return ans;
    ok[r][c1] = true;

    if (rtot[r] == 2)
        ans = solve(r + 1, c0);
    else if (rtot[r] == 1)
    {
        ans = 1LL * solve(r + 1, c0 - 1) * c0 % mod;
        ans = (ans + 1LL * solve(r + 1, c0) * c1) % mod;
    }
    else
    {
        ans = 1LL * solve(r + 1, c0 - 2) * c0 * (c0 - 1) / 2 % mod;
        ans = (ans + 1LL * solve(r + 1, c0 - 1) * c1 * c0) % mod;
        ans = (ans + 1LL * solve(r + 1, c0) * c1 * (c1 - 1) / 2) % mod;
    }

    return ans;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m >> mod;

    for (int i = 0; i < m; ++i)
    {
        string s;
        cin >> s;
        for (int j = 0; j < n; ++j)
            if (s[j] == '1')
                ++rtot[i], ++ctot[j];
    }

    for (int i = 0; i < m; ++i)
        if (rtot[i] > 2)
        {
            cout << 0 << endl;
            return 0;
        }

    int c[2] = { 0, 0 };
    for (int i = 0; i < n; ++i)
        if (ctot[i] > 2)
        {
            cout << 0 << endl;
            return 0;
        }
        else if (ctot[i] < 2) c[ctot[i]]++;

    for (int i = n - 1; i >= 0; --i)
        sum[i] = sum[i + 1] + 2 - rtot[i];

    cout << solve(0, c[0]) << endl;

    return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
char board[550][550];
ll dp[1201][1201];
int cnt[550];
ll memo(ll n2, ll n1) {
  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += memo(n2 - 2, n1 + 2) % mod * n2 * (n2 - 1) / 2 % mod;
  }
  if (n1 >= 2) {
    res += memo(n2, n1 - 2) % mod * n1 * (n1 - 1) / 2 % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += memo(n2 - 1, n1) % mod * n1 * n2 % mod;
  }
  return dp[n2][n1] = res;
}
string SS;
int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  for (int i = 0; i < n; i++){
    SS += "2";
  }
  for (int i = 0; i < m; i++){
    for (int j = 0; j < n; j++){
        SS[j] -= board[i][j] == '1';
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int x = 0; x < n; x++){
    n1 += SS[x] == '1';
    n2 += SS[x] == '2';
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
char board[550][550];
ll dp[550][550];
int cnt_col[550][550];
ll memo(int n2, int n1) {
  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += memo(n2 - 2, n1 + 2) * n2 * (n2 - 1) / 2 % mod;
  }
  if (n1 >= 2) {
    res += memo(n2, n1 - 2) * n1 * (n1 - 1) / 2 % mod;
  }
  if (n1 >= 1 and n2 >= 1){
    res += memo(n2 - 1, n1) * n1 * n2 % mod;
  }
  return dp[n2][n1] = res;
}
int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (i - 1 >= 0) {
        cnt_col[i][j] += cnt_col[i - 1][j];
      }
      if (board[i][j] == '1') {
        cnt_col[i][j]++;
      }
    }
  }
  int n2, n1;
  for (int i = 0; i < n; i++) {
    if (cnt_col[m - 1][i] > 2) {
      cout << 0 << endl;
      return 0;
    }
    if (cnt_col[m - 1][i] == 0) {
      n2++;
    }
    if (cnt_col[m - 1][i] == 1) {
      n1++;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
string board[550];
ll dp[1201][1201];
int num[550];
ll memo(ll n2, ll n1) {

  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += n2 * (n2 - 1) / 2 * memo(n2 - 2, n1 + 2) % mod;
  }
  if (n1 >= 2) {
    res += n1 * (n1 - 1) / 2 * memo(n2, n1 - 2) % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += n1 * n2 * memo(n2 - 1, n1) % mod;
  }
  return dp[n2][n1] = res;
}

int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    cin >> board[i];
    if (count(board[i].begin(), board[i].end(), '1') != 2) {
      cout << 0 << endl;
      return 0;
    }
    for (int j = 0; j < n; j++) {
      num[j] += board[i][j] == '1';
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int j = 0; j < n; j++) {
    n1 += num[j] == 1;
    n2 += num[j] == 0;
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) % mod << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
char board[550][550];
ll dp[1201][1201];
int cnt_col[550][550];
ll memo(ll n2, ll n1) {
  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += memo(n2 - 2, n1 + 2) % mod * n2 * (n2 - 1) / 2 % mod;
  }
  if (n1 >= 2) {
    res += memo(n2, n1 - 2) % mod * n1 * (n1 - 1) / 2 % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += memo(n2 - 1, n1) % mod * n1 * n2 % mod;
  }
  return dp[n2][n1] = res;
}
int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  memset(cnt_col, 0, sizeof(cnt_col));
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (i - 1 >= 0) {
        cnt_col[i][j] += cnt_col[i - 1][j];
      }
      if (board[i][j] == '1') {
        cnt_col[i][j]++;
      }
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int i = 0; i < n; i++) {
    if (cnt_col[m - 1][i] > 2) {
      cout << 0 << endl;
      return 0;
    }
    if (cnt_col[m - 1][i] == 0) {
      n2++;
    }
    if (cnt_col[m - 1][i] == 1) {
      n1++;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
string board[550];
ll dp[1201][1201];
int num[550];
ll memo(ll n2, ll n1) {

  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += n2 * (n2 - 1) / 2 * memo(n2 - 2, n1 + 2) % mod;
  }
  if (n1 >= 2) {
    res += n1 * (n1 - 1) / 2 * memo(n2, n1 - 2) % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += n1 * n2 * memo(n2 - 1, n1) % mod;
  }
  return dp[n2][n1] = res;
}

int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    cin >> board[i];
    if (count(board[i].begin(), board[i].end(), '1') != 2) {
      cout << 0 << endl;
      return 0;
    }
    for (int j = 0; j < n; j++) {
      num[j] += board[i][j] == '1';
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int j = 0; j < n; j++) {
    n1 += num[j] == 1;
    n2 += num[j] == 0;
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
char board[550][550];
ll dp[1201][1201];
int cnt[550];
ll memo(ll n2, ll n1) {
  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += memo(n2 - 2, n1 + 2) % mod * n2 * (n2 - 1) / 2 % mod;
  }
  if (n1 >= 2) {
    res += memo(n2, n1 - 2) % mod * n1 * (n1 - 1) / 2 % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += memo(n2 - 1, n1) % mod * n1 * n2 % mod;
  }
  return dp[n2][n1] = res;
}
string SS;
int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  for (int i = 0; i < n; i++){
    SS += "2";
  }
  for (int i = 0; i < m; i++){
    for (int j = 0; j < n; j++){
        SS[j] -= board[i][j] == '1';
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int x = 0; x < n; x++){
    n1 += SS[x] == 1;
    n2 += SS[x] == 0;
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
string board[550];
ll dp[1201][1201];
int num[550];
ll memo(ll n2, ll n1) {

  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += n2 * (n2 - 1) / 2 * memo(n2 - 2, n1 + 2) % mod;
  }
  if (n1 >= 2) {
    res += n1 * (n1 - 1) / 2 * memo(n2, n1 - 2) % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += n1 * n2 * memo(n2 - 1, n1) % mod;
  }
  return dp[n2][n1] = res;
}

int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    cin >> board[i];

       for (int j = 0; j < n; j++) {
      num[j] += board[i][j] == '1';
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int j = 0; j < n; j++) {
    n1 += num[j] == 1;
    n2 += num[j] == 0;
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) % mod << endl;
  return 0;
}


#include <iostream>
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <algorithm>
#include <functional>
#include <limits.h>
#include <bitset>

#include <tuple>
#include <unordered_map>

#define mp make_pair
#define mt make_tuple
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int n, m;
ll mod;
char board[550][550];
ll dp[1201][1201];
int cnt_col[550][550];
ll memo(ll n2, ll n1) {
  if (n2 + n1 == 1) {
    return 0;
  }
  if (n2 + n1 == 0) {
    return 1;
  }
  if (dp[n2][n1] >= 0) {
    return dp[n2][n1];
  }
  ll res = 0;
  if (n2 >= 2) {
    res += memo(n2 - 2, n1 + 2) % mod * n2 * (n2 - 1) / 2 % mod;
  }
  if (n1 >= 2) {
    res += memo(n2, n1 - 2) % mod * n1 * (n1 - 1) / 2 % mod;
  }
  if (n1 >= 1 and n2 >= 1) {
    res += memo(n2 - 1, n1) % mod * n1 * n2 % mod;
  }
  return dp[n2][n1] = res;
}
int main() {
  cin >> n >> m >> mod;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> board[i][j];
    }
  }
  memset(cnt_col, 0, sizeof(cnt_col));
  for (int j = 0; j < n; j++) {
    for (int i = 0; i < m; i++) {
      if (i - 1 >= 0) {
        cnt_col[i][j] += cnt_col[i - 1][j];
      }
      if (board[i][j] == '1') {
        cnt_col[i][j]++;
      }
    }
  }
  int n2, n1;
  n2 = n1 = 0;
  for (int i = 0; i < n; i++) {
    if (cnt_col[m - 1][i] > 2) {
      cout << 0 << endl;
      return 0;
    }
    if (cnt_col[m - 1][i] == 0) {
      n2++;
    }
    if (cnt_col[m - 1][i] == 1) {
      n1++;
    }
  }
  memset(dp, -1, sizeof(dp));
  cout << memo(n2, n1) % mod << endl;
  return 0;
}