#include <bits/stdc++.h>
using namespace std;
const long long MOD = 998244353;
const double PI = acos(-1.0);
const int N = 1e6 + 7;
inline long long qpow(long long a, long long b) {
  a %= MOD;
  long long c = 1;
  while (b) {
    if (b & 1) c = c * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return c;
}
long long fac[N], inv[N];
inline long long comb(long long n, long long m) {
  return fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
int main() {
  fac[0] = 1;
  for (int i = int(1); i <= int(N - 1); ++i) fac[i] = fac[i - 1] * i % MOD;
  inv[0] = inv[1] = 1;
  for (int i = int(2); i <= int(N - 1); ++i)
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
  for (int i = int(2); i <= int(N - 1); ++i) (inv[i] *= inv[i - 1]) %= MOD;
  int n;
  long long ans = 0;
  scanf("%d", &n);
  for (int i = int(1); i <= int(n); ++i) {
    ans += 2LL * comb(n, i) % MOD * (i & 1 ? 1 : -1) *
           qpow(3, 1LL * n * (n - i) + i) % MOD;
    if (ans >= MOD)
      ans -= MOD;
    else if (ans < 0)
      ans += MOD;
  }
  long long tmp;
  for (int i = int(0); i <= int(n - 1); ++i) {
    tmp = qpow(1 - qpow(3, i), n) - qpow(3, 1LL * i * n) * (n & 1 ? -1 : 1);
    if (tmp >= MOD)
      tmp -= MOD;
    else if (tmp < 0)
      tmp += MOD;
    ans += 3LL * comb(n, i) % MOD * (i & 1 ? 1 : -1) * tmp % MOD;
    if (ans >= MOD)
      ans -= MOD;
    else if (ans < 0)
      ans += MOD;
  }
  cout << ans << endl;
}
