#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
const long long mod = 998244353;
long long fac[N], inv[N];
long long pw(long long base, long long n) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = ans * base % mod;
    base = base * base % mod;
    n >>= 1;
  }
  return ans;
}
void init() {
  fac[0] = inv[0] = 1;
  for (int i = 1; i < N; i++) {
    fac[i] = fac[i - 1] * i % mod;
    inv[i] = pw(fac[i], mod - 2);
  }
}
long long C(int n, int m) { return fac[n] * inv[m] % mod * inv[n - m] % mod; }
long long sig(int n) { return (n & 1) ? -1LL : 1LL; }
void solve() {
  int n;
  scanf("%d", &n);
  long long ans = 0;
  long long tmp = 0;
  for (int i = 1; i <= n; i++) {
    tmp += C(n, i) * sig(i + 1) * pw(3, i) % mod * pw(3, 1LL * n * (n - i));
    tmp %= mod;
  }
  ans += 2 * tmp;
  ans %= mod;
  tmp = 0;
  for (int i = 0; i <= n - 1; i++) {
    long long t = pw(pw(3, i) - 1, n) - pw(3, 1LL * i * n);
    tmp += C(n, i) * sig(n - i + 1) * t;
    tmp %= mod;
  }
  ans += 3 * tmp;
  ans %= mod;
  (ans += mod) %= mod;
  cout << ans << endl;
}
int main() {
  init();
  solve();
  return 0;
}
